Merge branch 'master' of https://github.com/llongi/usb4java into llongi-master

This commit is contained in:
Klaus Reimer 2013-09-14 15:51:42 +02:00
commit 12b8622b83
112 changed files with 4412 additions and 2315 deletions

View File

@ -1,2 +0,0 @@
* Implement hotplug support.
* Implement asynchronous communication in low-level API.

View File

@ -6,7 +6,7 @@
<artifactId>usb4java</artifactId>
<packaging>jar</packaging>
<name>usb4java</name>
<version>1.1.0-SNAPSHOT</version>
<version>1.1.1-SNAPSHOT</version>
<url>http://kayahr.github.com/usb4java/</url>
<description>
USB library for Java based on libusb and implementing javax.usb (JSR-80).

View File

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

View File

@ -2,41 +2,98 @@ 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=""
UDEV_VERSION="1.2"
LIBUSB="stable"
LIBUSB_STABLE_VERSION="1.0.17"
LIBUSB_STABLE_RC=""
LIBUSB_BETA_VERSION="1.0.18"
LIBUSB_BETA_RC="-rc1"
build()
{
if [ "$LIBUSB" = "libusbx" ]
UDEV_NAME="eudev-$UDEV_VERSION"
UDEV_ARCHIVE="$UDEV_NAME.tar.gz"
UDEV_URL="http://dev.gentoo.org/~blueness/eudev/$UDEV_ARCHIVE"
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"
# Only Windows needs the shared library, the others want static ones.
if [ "$OS" = "windows" ]
then
LIB_CONFIG="--disable-static --enable-shared"
else
LIB_CONFIG="--enable-static --disable-shared"
fi
# Clean up
rm -rf "$TMPDIR"
rm -rf "$DISTDIR"
# Udev available only on Linux
if [ "$OS" = "linux" ]
then
if [ "$UDEV_SUPPORT" = "yes" ]
then
# Download udev if necessary
mkdir -p "$DOWNLOADS"
if [ ! -e "$DOWNLOADS/$UDEV_ARCHIVE" ]
then
if type curl >/dev/null 2>&1
then
curl -L -o "$DOWNLOADS/$UDEV_ARCHIVE" "$UDEV_URL"
else
wget -O "$DOWNLOADS/$UDEV_ARCHIVE" "$UDEV_URL"
fi
fi
UDEV_CONFIG="--enable-split-usr --disable-gtk-doc --disable-manpages --disable-gudev \
--disable-introspection --disable-keymap --disable-libkmod --disable-modules \
--disable-selinux --disable-rule-generator --disable-blkid $UDEV_CONFIG"
# Unpack and compile udev
mkdir -p "$TMPDIR"
cd "$TMPDIR"
tar xfz "$DOWNLOADS/$UDEV_ARCHIVE"
cd "$UDEV_NAME"
LIBS="$UDEV_LIBS" \
CFLAGS="$CFLAGS $UDEV_CFLAGS" \
./configure --prefix="$TMPDIR" --host="$HOST" --with-pic $LIB_CONFIG $UDEV_CONFIG
make
make install-strip
# Enable udev support if selected
LIBUSB_CONFIG="--enable-udev $LIBUSB_CONFIG"
else
# Disable udev support if not selected
LIBUSB_CONFIG="--disable-udev $LIBUSB_CONFIG"
fi
fi
# Download libusb if necessary
mkdir -p "$DOWNLOADS"
if [ ! -e "$DOWNLOADS/$LIBUSB_ARCHIVE" ]
then
if type curl >/dev/null 2>&1
then
curl -L -o "$DOWNLOADS/$LIBUSB_ARCHIVE" "$LIBUSB_URL"
else
wget -O "$DOWNLOADS/$LIBUSB_ARCHIVE" "$LIBUSB_URL"
fi
if type curl >/dev/null 2>&1
then
curl -L -o "$DOWNLOADS/$LIBUSB_ARCHIVE" "$LIBUSB_URL"
else
wget -O "$DOWNLOADS/$LIBUSB_ARCHIVE" "$LIBUSB_URL"
fi
fi
# Unpack and compile libusb
@ -44,8 +101,10 @@ build()
cd "$TMPDIR"
tar xfj "$DOWNLOADS/$LIBUSB_ARCHIVE"
cd "$LIBUSB_NAME"
PKG_CONFIG_PATH="$TMPDIR/lib/pkgconfig" \
LIBS="$LIBUSB_LIBS" \
CFLAGS="$CFLAGS $LIBUSB_CFLAGS" \
./configure --prefix="$TMPDIR" --host="$HOST" --with-pic $LIBUSB_CONFIG
./configure --prefix="$TMPDIR" --host="$HOST" --with-pic $LIB_CONFIG $LIBUSB_CONFIG
make
make install-strip
@ -53,7 +112,7 @@ build()
cd "$SRCDIR"
if [ ! -e configure ]
then
./autogen.sh
./autogen.sh
fi
# Build libusb4java
@ -62,7 +121,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,7 +9,7 @@ set -e
OS="linux"
ARCH="arm"
HOST="$ARCH-$OS-gnueabi"
LIBUSB_CONFIG="--disable-shared"
USB4JAVA_LIBS="-lrt"
CFLAGS="-Os"
UDEV_SUPPORT="no"
build

View File

@ -9,8 +9,7 @@ set -e
OS="linux"
ARCH="x86"
HOST="$ARCH-$OS-gnu"
CFLAGS="-m32"
LIBUSB_CONFIG="--disable-shared --disable-udev"
USB4JAVA_LIBS="-lrt"
CFLAGS="-m32 -O2"
UDEV_SUPPORT="yes"
build

View File

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

View File

@ -9,9 +9,8 @@ set -e
OS="osx"
ARCH="x86"
CFLAGS="-arch i686"
LIBUSB_CONFIG="--disable-shared"
HOST="i686-apple-darwin"
CFLAGS="-arch i686 -O2"
USB4JAVA_LIBS="-lobjc -Wl,-framework,IOKit -Wl,-framework,CoreFoundation"
LIBUSB="libusb"
build

View File

@ -9,9 +9,8 @@ set -e
OS="osx"
ARCH="x86_64"
CFLAGS="-arch x86_64"
LIBUSB_CONFIG="--disable-shared"
HOST="$ARCH-apple-darwin"
CFLAGS="-arch x86_64 -O2"
USB4JAVA_LIBS="-lobjc -Wl,-framework,IOKit -Wl,-framework,CoreFoundation"
LIBUSB="libusb"
build

View File

@ -9,6 +9,6 @@ set -e
OS="windows"
ARCH="x86"
HOST="i686-w64-mingw32"
CFLAGS="-m32 -Wl,--add-stdcall-alias"
CFLAGS="-m32 -O2 -Wl,--add-stdcall-alias"
build

View File

@ -9,6 +9,6 @@ set -e
OS="windows"
ARCH="x86_64"
HOST="$ARCH-w64-mingw32"
CFLAGS="-m64 -Wl,--add-stdcall-alias"
CFLAGS="-m64 -O2 -Wl,--add-stdcall-alias"
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 "BosDevCapabilityDescriptor.h"
void setBosDescriptor(JNIEnv* env,
struct libusb_bos_descriptor* descriptor, jobject object)
const struct libusb_bos_descriptor* descriptor, jobject object)
{
SET_POINTER(env, descriptor, object, "bosDescriptorPointer");
}
@ -35,7 +35,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(BosDescriptor, bLength)
struct libusb_bos_descriptor *descriptor =
unwrapBosDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bLength;
return (jbyte) descriptor->bLength;
}
/**
@ -49,7 +49,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(BosDescriptor, bDescriptorType)
struct libusb_bos_descriptor *descriptor =
unwrapBosDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDescriptorType;
return (jbyte) descriptor->bDescriptorType;
}
/**
@ -63,7 +63,7 @@ JNIEXPORT jshort JNICALL METHOD_NAME(BosDescriptor, wTotalLength)
struct libusb_bos_descriptor *descriptor =
unwrapBosDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->wTotalLength;
return (jshort) descriptor->wTotalLength;
}
/**
@ -77,7 +77,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(BosDescriptor, bNumDeviceCaps)
struct libusb_bos_descriptor *descriptor =
unwrapBosDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bNumDeviceCaps;
return (jbyte) descriptor->bNumDeviceCaps;
}
/**

View File

@ -8,7 +8,7 @@
#include "usb4java.h"
void setBosDescriptor(JNIEnv*, struct libusb_bos_descriptor*, jobject);
void setBosDescriptor(JNIEnv*, const struct libusb_bos_descriptor*, jobject);
struct libusb_bos_descriptor* unwrapBosDescriptor(JNIEnv*, jobject);
void resetBosDescriptor(JNIEnv*, jobject);

View File

@ -13,21 +13,21 @@ jobject wrapBosDevCapabilityDescriptor(JNIEnv *env,
}
jobjectArray wrapBosDevCapabilityDescriptors(JNIEnv *env, int count,
struct libusb_bos_dev_capability_descriptor **descriptors)
struct libusb_bos_dev_capability_descriptor * const *descriptors)
{
int i;
jobjectArray array = (jobjectArray) (*env)->NewObjectArray(env,
count, (*env)->FindClass(env, PACKAGE_DIR"/BosDevCapabilityDescriptor"),
NULL);
for (i = 0; i < count; i++)
for (int i = 0; i < count; i++)
(*env)->SetObjectArrayElement(env, array, i,
wrapBosDevCapabilityDescriptor(env, descriptors[i]));
return array;
}
struct libusb_bos_dev_capability_descriptor
*unwrapBosDevCapabilityDescriptor(JNIEnv *env, jobject obj)
struct libusb_bos_dev_capability_descriptor*
unwrapBosDevCapabilityDescriptor(JNIEnv *env, jobject obj)
{
UNWRAP_POINTER(env, obj, struct libusb_bos_dev_capability_descriptor*,
"bosDevCapabilityDescriptorPointer");
@ -44,7 +44,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(BosDevCapabilityDescriptor, bLength)
struct libusb_bos_dev_capability_descriptor* descriptor =
unwrapBosDevCapabilityDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bLength;
return (jbyte) descriptor->bLength;
}
/**
@ -59,7 +59,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(BosDevCapabilityDescriptor,
struct libusb_bos_dev_capability_descriptor* descriptor =
unwrapBosDevCapabilityDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDescriptorType;
return (jbyte) descriptor->bDescriptorType;
}
/**
@ -74,7 +74,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(BosDevCapabilityDescriptor,
struct libusb_bos_dev_capability_descriptor* descriptor =
unwrapBosDevCapabilityDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDevCapabilityType;
return (jbyte) descriptor->bDevCapabilityType;
}
/**
@ -89,6 +89,5 @@ JNIEXPORT jobject JNICALL METHOD_NAME(BosDevCapabilityDescriptor,
struct libusb_bos_dev_capability_descriptor *descriptor =
unwrapBosDevCapabilityDescriptor(env, this);
if (!descriptor) return NULL;
return (*env)->NewDirectByteBuffer(env, (void *)
descriptor->dev_capability_data, descriptor->bLength - 3);
return NewDirectReadOnlyByteBuffer(env, descriptor->dev_capability_data, descriptor->bLength - 3);
}

View File

@ -8,9 +8,11 @@
#include "usb4java.h"
jobject wrapBosDevCapabilityDescriptor(JNIEnv *,
const struct libusb_bos_dev_capability_descriptor *);
jobjectArray wrapBosDevCapabilityDescriptors(JNIEnv*, int,
struct libusb_bos_dev_capability_descriptor**);
struct libusb_bos_dev_capability_descriptor
*unwrapBosDevCapabilityDescriptor(JNIEnv *, jobject);
struct libusb_bos_dev_capability_descriptor * const *);
struct libusb_bos_dev_capability_descriptor*
unwrapBosDevCapabilityDescriptor(JNIEnv *, jobject);
#endif

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

@ -6,7 +6,7 @@
#include "ContainerIdDescriptor.h"
void setContainerIdDescriptor(JNIEnv* env,
struct libusb_container_id_descriptor* descriptor, jobject object)
const struct libusb_container_id_descriptor* descriptor, jobject object)
{
SET_POINTER(env, descriptor, object, "containerIdDescriptorPointer");
}
@ -35,7 +35,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ContainerIdDescriptor, bLength)
struct libusb_container_id_descriptor *descriptor =
unwrapContainerIdDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bLength;
return (jbyte) descriptor->bLength;
}
/**
@ -49,7 +49,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ContainerIdDescriptor, bDescriptorType)
struct libusb_container_id_descriptor *descriptor =
unwrapContainerIdDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDescriptorType;
return (jbyte) descriptor->bDescriptorType;
}
/**
@ -63,7 +63,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ContainerIdDescriptor, bDevCapabilityType)
struct libusb_container_id_descriptor *descriptor =
unwrapContainerIdDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDevCapabilityType;
return (jbyte) descriptor->bDevCapabilityType;
}
/**
@ -77,7 +77,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ContainerIdDescriptor, bReserved)
struct libusb_container_id_descriptor *descriptor =
unwrapContainerIdDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bReserved;
return (jbyte) descriptor->bReserved;
}
/**
@ -91,6 +91,5 @@ JNIEXPORT jobject JNICALL METHOD_NAME(ContainerIdDescriptor, containerId)
struct libusb_container_id_descriptor *descriptor =
unwrapContainerIdDescriptor(env, this);
if (!descriptor) return NULL;
return (*env)->NewDirectByteBuffer(env, (void *) descriptor->ContainerID,
16);
return NewDirectReadOnlyByteBuffer(env, descriptor->ContainerID, 16);
}

View File

@ -9,7 +9,7 @@
#include "usb4java.h"
void setContainerIdDescriptor(JNIEnv*,
struct libusb_container_id_descriptor*, jobject);
const struct libusb_container_id_descriptor*, jobject);
struct libusb_container_id_descriptor*
unwrapContainerIdDescriptor(JNIEnv*, jobject);
void resetContainerIdDescriptor(JNIEnv*, jobject);

View File

@ -5,11 +5,16 @@
#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");
}
jobject wrapContext(JNIEnv* env, const libusb_context* context)
{
WRAP_POINTER(env, context, "Context", "contextPointer");
}
libusb_context* unwrapContext(JNIEnv* env, jobject context)
{
UNWRAP_POINTER(env, context, libusb_context*, "contextPointer");

View File

@ -8,7 +8,8 @@
#include "usb4java.h"
void setContext(JNIEnv*, libusb_context*, jobject);
void setContext(JNIEnv*, const libusb_context*, jobject);
jobject wrapContext(JNIEnv*, const libusb_context*);
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)
{
UNWRAP_DATA(env, descriptor, struct libusb_device_descriptor*,
"deviceDescriptorData");
UNWRAP_POINTER(env, descriptor, 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,12 +5,13 @@
#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");
}

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* const *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

@ -0,0 +1,29 @@
/*
* Copyright (C) 2013 Luca Longinotti (l@longi.li)
* See COPYING file for copying conditions
*/
#include "HotplugCallbackHandle.h"
void setHotplugCallbackHandle(JNIEnv* env, const libusb_hotplug_callback_handle hotplugHandle,
jobject object)
{
SET_POINTER(env, hotplugHandle, object, "hotplugCallbackHandleValue");
}
libusb_hotplug_callback_handle unwrapHotplugCallbackHandle(JNIEnv* env, jobject hotplugHandle)
{
// Hotplug callback handles are integers, starting at 1. As such, we can use the same logic
// as for pointers, and consider 0 to be uninitialized/invalid.
if (!hotplugHandle) return 0;
jclass cls = (*env)->GetObjectClass(env, hotplugHandle);
jfieldID field = (*env)->GetFieldID(env, cls, "hotplugCallbackHandleValue", "J");
jlong val = (*env)->GetLongField(env, hotplugHandle, field);
if (!val) illegalState(env, "hotplugCallbackHandleValue is not initialized");
return (libusb_hotplug_callback_handle) val;
}
void resetHotplugCallbackHandle(JNIEnv* env, jobject hotplugHandle)
{
RESET_POINTER(env, hotplugHandle, "hotplugCallbackHandleValue");
}

View File

@ -0,0 +1,15 @@
/*
* Copyright (C) 2013 Luca Longinotti (l@longi.li)
* See COPYING file for copying conditions
*/
#ifndef USB4JAVA_HOTPLUG_CALLBACK_HANDLE_H
#define USB4JAVA_HOTPLUG_CALLBACK_HANDLE_H
#include "usb4java.h"
void setHotplugCallbackHandle(JNIEnv*, const libusb_hotplug_callback_handle, jobject);
libusb_hotplug_callback_handle unwrapHotplugCallbackHandle(JNIEnv*, jobject);
void resetHotplugCallbackHandle(JNIEnv*, jobject);
#endif

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,93 @@
/*
* Copyright (C) 2013 Luca Longinotti (l@longi.li)
* See COPYING file for copying conditions
*/
#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,17 @@
/*
* Copyright (C) 2013 Luca Longinotti (l@longi.li)
* See COPYING file for copying conditions
*/
#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,10 +17,12 @@ libusb4java_la_SOURCES = \
Interface.c \
InterfaceDescriptor.c \
EndpointDescriptor.c \
IsoPacketDescriptor.c \
Transfer.c \
SsEndpointCompanionDescriptor.c \
BosDescriptor.c \
BosDevCapabilityDescriptor.c \
Usb20ExtensionDescriptor.c \
SsUsbDeviceCapabilityDescriptor.c \
ContainerIdDescriptor.c
ContainerIdDescriptor.c \
HotplugCallbackHandle.c

View File

@ -7,9 +7,9 @@
#include "Interface.h"
void setSsEndpointCompanionDescriptor(JNIEnv* env,
struct libusb_ss_endpoint_companion_descriptor* descriptor, jobject object)
const struct libusb_ss_endpoint_companion_descriptor* descriptor, jobject object)
{
SET_POINTER(env, descriptor, object, "ssEndpointCompanionDescriptor");
SET_POINTER(env, descriptor, object, "ssEndpointCompanionDescriptorPointer");
}
struct libusb_ss_endpoint_companion_descriptor*
@ -17,12 +17,12 @@ struct libusb_ss_endpoint_companion_descriptor*
{
UNWRAP_POINTER(env, descriptor,
struct libusb_ss_endpoint_companion_descriptor*,
"ssEndpointCompanionDescriptor");
"ssEndpointCompanionDescriptorPointer");
}
void resetSsEndpointCompanionDescriptor(JNIEnv* env, jobject obj)
{
RESET_POINTER(env, obj, "ssEndpointCompanionDescriptor");
RESET_POINTER(env, obj, "ssEndpointCompanionDescriptorPointer");
}
/**
@ -36,7 +36,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(SsEndpointCompanionDescriptor, bLength)
struct libusb_ss_endpoint_companion_descriptor *descriptor =
unwrapSsEndpointCompanionDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bLength;
return (jbyte) descriptor->bLength;
}
/**
@ -51,7 +51,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(SsEndpointCompanionDescriptor,
struct libusb_ss_endpoint_companion_descriptor *descriptor =
unwrapSsEndpointCompanionDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDescriptorType;
return (jbyte) descriptor->bDescriptorType;
}
/**
@ -65,7 +65,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(SsEndpointCompanionDescriptor, bMaxBurst)
struct libusb_ss_endpoint_companion_descriptor *descriptor =
unwrapSsEndpointCompanionDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bMaxBurst;
return (jbyte) descriptor->bMaxBurst;
}
/**
@ -79,7 +79,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(SsEndpointCompanionDescriptor, bmAttributes)
struct libusb_ss_endpoint_companion_descriptor *descriptor =
unwrapSsEndpointCompanionDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bmAttributes;
return (jbyte) descriptor->bmAttributes;
}
/**
@ -93,5 +93,5 @@ JNIEXPORT jshort JNICALL METHOD_NAME(SsEndpointCompanionDescriptor, wBytesPerInt
struct libusb_ss_endpoint_companion_descriptor *descriptor =
unwrapSsEndpointCompanionDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->wBytesPerInterval;
return (jshort) descriptor->wBytesPerInterval;
}

View File

@ -9,7 +9,7 @@
#include "usb4java.h"
void setSsEndpointCompanionDescriptor(JNIEnv*,
struct libusb_ss_endpoint_companion_descriptor*, jobject);
const struct libusb_ss_endpoint_companion_descriptor*, jobject);
struct libusb_ss_endpoint_companion_descriptor*
unwrapSsEndpointCompanionDescriptor(JNIEnv*, jobject);
void resetSsEndpointCompanionDescriptor(JNIEnv*, jobject);

View File

@ -6,7 +6,7 @@
#include "SsUsbDeviceCapabilityDescriptor.h"
void setSsUsbDeviceCapabilityDescriptor(JNIEnv* env,
struct libusb_ss_usb_device_capability_descriptor* descriptor, jobject object)
const struct libusb_ss_usb_device_capability_descriptor* descriptor, jobject object)
{
SET_POINTER(env, descriptor, object, "ssUsbDeviceCapabilityDescriptorPointer");
}
@ -35,7 +35,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(SsUsbDeviceCapabilityDescriptor, bLength)
struct libusb_ss_usb_device_capability_descriptor *descriptor =
unwrapSsUsbDeviceCapabilityDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bLength;
return (jbyte) descriptor->bLength;
}
/**
@ -49,7 +49,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(SsUsbDeviceCapabilityDescriptor, bDescriptor
struct libusb_ss_usb_device_capability_descriptor *descriptor =
unwrapSsUsbDeviceCapabilityDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDescriptorType;
return (jbyte) descriptor->bDescriptorType;
}
/**
@ -64,11 +64,11 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(SsUsbDeviceCapabilityDescriptor,
struct libusb_ss_usb_device_capability_descriptor *descriptor =
unwrapSsUsbDeviceCapabilityDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDevCapabilityType;
return (jbyte) descriptor->bDevCapabilityType;
}
/**
* int bmAttributes()
* byte bmAttributes()
*/
JNIEXPORT jbyte JNICALL METHOD_NAME(SsUsbDeviceCapabilityDescriptor,
bmAttributes)
@ -79,7 +79,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(SsUsbDeviceCapabilityDescriptor,
struct libusb_ss_usb_device_capability_descriptor *descriptor =
unwrapSsUsbDeviceCapabilityDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bmAttributes;
return (jbyte) descriptor->bmAttributes;
}
/**
@ -94,7 +94,7 @@ JNIEXPORT jshort JNICALL METHOD_NAME(SsUsbDeviceCapabilityDescriptor,
struct libusb_ss_usb_device_capability_descriptor *descriptor =
unwrapSsUsbDeviceCapabilityDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->wSpeedSupported;
return (jshort) descriptor->wSpeedSupported;
}
/**
@ -109,7 +109,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(SsUsbDeviceCapabilityDescriptor,
struct libusb_ss_usb_device_capability_descriptor *descriptor =
unwrapSsUsbDeviceCapabilityDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bFunctionalitySupport;
return (jbyte) descriptor->bFunctionalitySupport;
}
/**
@ -124,7 +124,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(SsUsbDeviceCapabilityDescriptor,
struct libusb_ss_usb_device_capability_descriptor *descriptor =
unwrapSsUsbDeviceCapabilityDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bU1DevExitLat;
return (jbyte) descriptor->bU1DevExitLat;
}
/**
@ -139,5 +139,5 @@ JNIEXPORT jshort JNICALL METHOD_NAME(SsUsbDeviceCapabilityDescriptor,
struct libusb_ss_usb_device_capability_descriptor *descriptor =
unwrapSsUsbDeviceCapabilityDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bU2DevExitLat;
return (jshort) descriptor->bU2DevExitLat;
}

View File

@ -9,7 +9,7 @@
#include "usb4java.h"
void setSsUsbDeviceCapabilityDescriptor(JNIEnv*,
struct libusb_ss_usb_device_capability_descriptor*, jobject);
const struct libusb_ss_usb_device_capability_descriptor*, jobject);
struct libusb_ss_usb_device_capability_descriptor*
unwrapSsUsbDeviceCapabilityDescriptor(JNIEnv*, jobject);
void resetSsUsbDeviceCapabilityDescriptor(JNIEnv*, jobject);

View File

@ -1,24 +1,36 @@
/*
* Copyright (C) 2013 Klaus Reimer (k@ailis.de)
* Copyright (C) 2013 Luca Longinotti (l@longi.li)
* 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 +41,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 +155,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

@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Klaus Reimer (k@ailis.de)
* Copyright (C) 2013 Luca Longinotti (l@longi.li)
* See COPYING file for copying conditions
*/
@ -8,7 +9,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

@ -6,7 +6,7 @@
#include "Usb20ExtensionDescriptor.h"
void setUsb20ExtensionDescriptor(JNIEnv* env,
struct libusb_usb_2_0_extension_descriptor* descriptor, jobject object)
const struct libusb_usb_2_0_extension_descriptor* descriptor, jobject object)
{
SET_POINTER(env, descriptor, object, "usb20ExtensionDescriptorPointer");
}
@ -35,7 +35,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(Usb20ExtensionDescriptor, bLength)
struct libusb_usb_2_0_extension_descriptor *descriptor =
unwrapUsb20ExtensionDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bLength;
return (jbyte) descriptor->bLength;
}
/**
@ -49,7 +49,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(Usb20ExtensionDescriptor, bDescriptorType)
struct libusb_usb_2_0_extension_descriptor *descriptor =
unwrapUsb20ExtensionDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDescriptorType;
return (jbyte) descriptor->bDescriptorType;
}
/**
@ -64,7 +64,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(Usb20ExtensionDescriptor,
struct libusb_usb_2_0_extension_descriptor *descriptor =
unwrapUsb20ExtensionDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDevCapabilityType;
return (jbyte) descriptor->bDevCapabilityType;
}
/**
@ -79,5 +79,5 @@ JNIEXPORT jint JNICALL METHOD_NAME(Usb20ExtensionDescriptor,
struct libusb_usb_2_0_extension_descriptor *descriptor =
unwrapUsb20ExtensionDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bmAttributes;
return (jint) descriptor->bmAttributes;
}

View File

@ -9,7 +9,7 @@
#include "usb4java.h"
void setUsb20ExtensionDescriptor(JNIEnv*,
struct libusb_usb_2_0_extension_descriptor*, jobject);
const struct libusb_usb_2_0_extension_descriptor*, jobject);
struct libusb_usb_2_0_extension_descriptor*
unwrapUsb20ExtensionDescriptor(JNIEnv*, jobject);
void resetUsb20ExtensionDescriptor(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

@ -1,19 +1,84 @@
/*
* Copyright (C) 2013 Klaus Reimer (k@ailis.de)
* Copyright (C) 2013 Luca Longinotti (l@longi.li)
* See COPYING file for copying conditions
*/
#include "usb4java.h"
jint illegalArgument(JNIEnv *env, char *message)
JavaVM *jvm = NULL;
jclass jClassLibUsb = NULL;
jmethodID jMethodTriggerPollfdAdded = NULL;
jmethodID jMethodTriggerPollfdRemoved = NULL;
jmethodID jMethodHotplugCallback = 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)
{
// Set JVM to the current one.
jvm = vm;
// Get the current environment.
JNIEnv *env;
jint getEnvResult = (*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6);
if (getEnvResult != JNI_OK)
{
// Send unrecognized version to signal error and deny library load.
return -1;
}
// Find classes and methods and cache them.
// Persistence is guaranteed by global references.
jClassLibUsb = (*env)->FindClass(env, PACKAGE_DIR"/LibUsb");
jClassLibUsb = (*env)->NewGlobalRef(env, jClassLibUsb);
jMethodTriggerPollfdAdded = (*env)->GetStaticMethodID(env, jClassLibUsb,
"triggerPollfdAdded", "(Ljava/io/FileDescriptor;IJ)V");
jMethodTriggerPollfdRemoved = (*env)->GetStaticMethodID(env, jClassLibUsb,
"triggerPollfdRemoved", "(Ljava/io/FileDescriptor;J)V");
jMethodHotplugCallback = (*env)->GetStaticMethodID(env, jClassLibUsb,
"hotplugCallback", "(L"PACKAGE_DIR"/Context;L"PACKAGE_DIR"/Device;IJ)I");
return JNI_VERSION_1_6;
}
void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
{
// Get the current environment.
JNIEnv *env;
jint getEnvResult = (*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6);
if (getEnvResult != JNI_OK)
{
return;
}
// Cleanup all global references.
(*env)->DeleteGlobalRef(env, jClassLibUsb);
}

View File

@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Klaus Reimer (k@ailis.de)
* Copyright (C) 2013 Luca Longinotti (l@longi.li)
* See COPYING file for copying conditions
*/
@ -20,21 +21,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 +39,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)) \
// 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,16 +65,38 @@
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); \
jint getEnvResult = (*jvm)->GetEnv(jvm, (void **) &ENV, JNI_VERSION_1_6); \
if (getEnvResult == JNI_EDETACHED) \
(*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);
// JVM access.
extern JavaVM *jvm;
// Callback caching.
extern jclass jClassLibUsb;
extern jmethodID jMethodTriggerPollfdAdded;
extern jmethodID jMethodTriggerPollfdRemoved;
extern jmethodID jMethodHotplugCallback;
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

@ -6,7 +6,7 @@
#include <string.h>
// Enforce usage of older memcpy to be compatible with older libc versions
#if WRAP_MEMCPY
#ifdef WRAP_MEMCPY
asm (".symver memcpy, memcpy@GLIBC_2.2.5");
void *__wrap_memcpy(void *dest, const void *src, size_t n)
{

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

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -23,10 +23,10 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* A structure representing the Binary Device Object Store (BOS) descriptor.
*
*
* This descriptor is documented in section 9.6.2 of the USB 3.0 specification.
* All multiple-byte fields are represented in host-endian format.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class BosDescriptor
@ -36,8 +36,7 @@ public final class BosDescriptor
/**
* Constructs a new BOS descriptor which can be passed to the
* {@link LibUsb#getBosDescriptor(DeviceHandle, BosDescriptor)}
* method.
* {@link LibUsb#getBosDescriptor(DeviceHandle, BosDescriptor)} method.
*/
public BosDescriptor()
{
@ -46,7 +45,7 @@ public final class BosDescriptor
/**
* Returns the native pointer.
*
*
* @return The native pointer.
*/
public long getPointer()
@ -56,94 +55,109 @@ public final class BosDescriptor
/**
* Returns the size of this descriptor (in bytes).
*
*
* @return The descriptor size in bytes;
*/
public native byte bLength();
/**
* Returns the descriptor type.
*
*
* @return The descriptor type.
*/
public native byte bDescriptorType();
/**
* Returns the length of this descriptor and all of its sub descriptors.
*
* Returns the length of this descriptor and all of its sub descriptors.
*
* @return The total descriptor length.
*/
public native short wTotalLength();
/**
* Returns the number of separate device capability descriptors in the BOS.
*
* Returns the number of separate device capability descriptors in the BOS.
*
* @return The number of device capability descriptors.
*/
public native byte bNumDeviceCaps();
/**
* Returns the array with the device capability descriptors.
*
*
* @return The array with device capability descriptors.
*/
public native BosDevCapabilityDescriptor[] devCapability();
/**
* Returns a dump of this descriptor.
*
*
* @return The descriptor dump.
*/
public String dump()
{
final StringBuilder builder = new StringBuilder();
builder.append(String.format("BOS Descriptor:%n"
+ " bLength %18d%n"
+ " bDescriptorType %10d%n"
+ " wTotalLength %13s%n"
+ " bNumDeviceCaps %11s%n",
bLength() & 0xff,
bDescriptorType() & 0xff,
wTotalLength() & 0xffff,
bNumDeviceCaps() & 0xff));
for (final BosDevCapabilityDescriptor descriptor: devCapability())
builder.append(String.format(
"BOS Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" wTotalLength %13s%n" +
" bNumDeviceCaps %11s%n",
this.bLength() & 0xFF,
this.bDescriptorType() & 0xFF,
this.wTotalLength() & 0xFFFF,
this.bNumDeviceCaps() & 0xFF));
for (final BosDevCapabilityDescriptor descriptor : this.devCapability())
{
builder.append(descriptor.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 BosDescriptor other =
(BosDescriptor) obj;
return new EqualsBuilder()
.append(bDescriptorType(), other.bDescriptorType())
.append(bLength(), other.bLength())
.append(wTotalLength(), other.wTotalLength())
.append(bNumDeviceCaps(), other.bNumDeviceCaps())
.append(devCapability(), other.devCapability()).isEquals();
return builder.toString();
}
@Override
public int hashCode()
{
return new HashCodeBuilder()
.append(bLength())
.append(bDescriptorType())
.append(wTotalLength())
.append(bNumDeviceCaps())
.append(devCapability())
.append(this.bLength())
.append(this.bDescriptorType())
.append(this.wTotalLength())
.append(this.bNumDeviceCaps())
.append(this.devCapability())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (obj == null)
{
return false;
}
if (obj == this)
{
return true;
}
if (obj.getClass() != this.getClass())
{
return false;
}
final BosDescriptor other = (BosDescriptor) obj;
return new EqualsBuilder()
.append(this.bLength(), other.bLength())
.append(this.bDescriptorType(), other.bDescriptorType())
.append(this.wTotalLength(), other.wTotalLength())
.append(this.bNumDeviceCaps(), other.bNumDeviceCaps())
.append(this.devCapability(), other.devCapability())
.isEquals();
}
@Override
public String toString()
{
return dump();
return this.dump();
}
}

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -27,10 +27,10 @@ import de.ailis.usb4java.utils.DescriptorUtils;
/**
* A generic representation of a BOS Device Capability descriptor.
*
*
* It is advised to check bDevCapabilityType and call the matching
* get*Descriptor method to get a structure fully matching the type.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class BosDevCapabilityDescriptor
@ -49,7 +49,7 @@ public final class BosDevCapabilityDescriptor
/**
* Returns the native pointer.
*
*
* @return The native pointer.
*/
public long getPointer()
@ -59,82 +59,92 @@ public final class BosDevCapabilityDescriptor
/**
* Returns the size of this descriptor (in bytes).
*
*
* @return The descriptor size in bytes;
*/
public native byte bLength();
/**
* Returns the descriptor type.
*
*
* @return The descriptor type.
*/
public native byte bDescriptorType();
/**
* Returns the device capability type.
*
*
* @return The device capability type.
*/
public native byte bDevCapabilityType();
/**
* Returns the device capability data (bLength - 3 bytes).
*
*
* @return The device capability data.
*/
public native ByteBuffer devCapabilityData();
/**
* Returns a dump of this descriptor.
*
*
* @return The descriptor dump.
*/
public String dump()
{
return String.format("BOS Device Capability Descriptor:%n"
+ " bLength %18d%n"
+ " bDescriptorType %10d%n"
+ " bDevCapabilityType %7s%n"
+ " devCapabilityData:%n%s%n",
bLength() & 0xff,
bDescriptorType() & 0xff,
bDevCapabilityType() & 0xff,
DescriptorUtils.dump(devCapabilityData())
return String.format(
"BOS Device Capability Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" bDevCapabilityType %7s%n" +
" devCapabilityData:%n%s%n",
this.bLength() & 0xFF,
this.bDescriptorType() & 0xFF,
this.bDevCapabilityType() & 0xFF,
DescriptorUtils.dump(this.devCapabilityData())
.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 BosDevCapabilityDescriptor other =
(BosDevCapabilityDescriptor) obj;
return new EqualsBuilder()
.append(bDescriptorType(), other.bDescriptorType())
.append(bLength(), other.bLength())
.append(bDevCapabilityType(), other.bDevCapabilityType())
.append(devCapabilityData().array(),
other.devCapabilityData().array()).isEquals();
}
@Override
public int hashCode()
{
return new HashCodeBuilder()
.append(bLength())
.append(bDescriptorType())
.append(bDevCapabilityType())
.append(devCapabilityData())
.append(this.bLength())
.append(this.bDescriptorType())
.append(this.bDevCapabilityType())
.append(this.devCapabilityData())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (obj == null)
{
return false;
}
if (obj == this)
{
return true;
}
if (obj.getClass() != this.getClass())
{
return false;
}
final BosDevCapabilityDescriptor other = (BosDevCapabilityDescriptor) obj;
return new EqualsBuilder()
.append(this.bLength(), other.bLength())
.append(this.bDescriptorType(), other.bDescriptorType())
.append(this.bDevCapabilityType(), other.bDevCapabilityType())
.append(this.devCapabilityData(), other.devCapabilityData()).
isEquals();
}
@Override
public String toString()
{
return dump();
return this.dump();
}
}

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -29,10 +29,10 @@ import de.ailis.usb4java.utils.DescriptorUtils;
/**
* A structure representing the standard USB configuration descriptor.
*
*
* This descriptor is documented in section 9.6.3 of the USB 3.0 specification.
* All multiple-byte fields are represented in host-endian format.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class ConfigDescriptor implements UsbConfigurationDescriptor
@ -51,7 +51,7 @@ public final class ConfigDescriptor implements UsbConfigurationDescriptor
/**
* Returns the native pointer.
*
*
* @return The native pointer.
*/
public long getPointer()
@ -85,103 +85,108 @@ public final class ConfigDescriptor implements UsbConfigurationDescriptor
/**
* Returns the array with interfaces supported by this configuration.
*
*
* @return The array with interfaces.
*/
public native Interface[] iface();
/**
* Extra descriptors.
*
*
* If libusb encounters unknown interface descriptors, it will store them
* here, should you wish to parse them.
*
*
* @return The extra descriptors.
*/
public native ByteBuffer extra();
/**
* Length of the extra descriptors, in bytes.
*
*
* @return The extra descriptors length.
*/
public native int extraLength();
/**
* Returns a dump of this descriptor.
*
*
* @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),
this.extraLength(),
DescriptorUtils.dump(this.extra()).replaceAll("(?m)^", " ")));
for (final Interface iface : this.iface())
{
builder.append("%n" + iface.dump());
}
return builder.toString();
}
@Override
public int hashCode()
{
return new HashCodeBuilder()
.append(bLength())
.append(bDescriptorType())
.append(wTotalLength())
.append(bNumInterfaces())
.append(bConfigurationValue())
.append(iConfiguration())
.append(bmAttributes())
.append(bMaxPower())
.append(extra())
.append(extraLength())
.append(this.bLength())
.append(this.bDescriptorType())
.append(this.wTotalLength())
.append(this.bNumInterfaces())
.append(this.bConfigurationValue())
.append(this.iConfiguration())
.append(this.bmAttributes())
.append(this.bMaxPower())
.append(this.iface())
.append(this.extra())
.append(this.extraLength())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (this.getClass() != obj.getClass())
{
return false;
}
final ConfigDescriptor other = (ConfigDescriptor) obj;
return new EqualsBuilder()
.append(this.bLength(), other.bLength())
.append(this.bDescriptorType(), other.bDescriptorType())
.append(this.wTotalLength(), other.wTotalLength())
.append(this.bNumInterfaces(), other.bNumInterfaces())
.append(this.bConfigurationValue(), other.bConfigurationValue())
.append(this.iConfiguration(), other.iConfiguration())
.append(this.bmAttributes(), other.bmAttributes())
.append(this.bMaxPower(), other.bMaxPower())
.append(this.iface(), other.iface())
.append(this.extra(), other.extra())
.append(this.extraLength(), other.extraLength())
.isEquals();
}
@Override
public String toString()
{
return dump();
return this.dump();
}
}

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -27,11 +27,11 @@ import de.ailis.usb4java.utils.DescriptorUtils;
/**
* A structure representing the Container ID descriptor.
*
*
* This descriptor is documented in section 9.6.2.3 of the USB 3.0
* specification. All multiple-byte fields, except UUIDs, are represented in
* host-endian format.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class ContainerIdDescriptor
@ -41,8 +41,7 @@ public final class ContainerIdDescriptor
/**
* Constructs a new Container Id descriptor which can be passed to the
* {@link LibUsb#getContainerIdDescriptor(Context,
* BosDevCapabilityDescriptor, ContainerIdDescriptor)}
* {@link LibUsb#getContainerIdDescriptor(Context, BosDevCapabilityDescriptor, ContainerIdDescriptor)}
* method.
*/
public ContainerIdDescriptor()
@ -52,7 +51,7 @@ public final class ContainerIdDescriptor
/**
* Returns the native pointer.
*
*
* @return The native pointer.
*/
public long getPointer()
@ -62,91 +61,103 @@ public final class ContainerIdDescriptor
/**
* Returns the size of this descriptor (in bytes).
*
*
* @return The descriptor size in bytes;
*/
public native byte bLength();
/**
* Returns the descriptor type.
*
*
* @return The descriptor type.
*/
public native byte bDescriptorType();
/**
* Returns the device capability type.
*
*
* @return The device capability type.
*/
public native byte bDevCapabilityType();
/**
* Returns the reserved field.
*
*
* @return The reserved field.
*/
public native byte bReserved();
/**
* Returns the 128 bit UUID.
*
*
* @return The 128 bit UUID.
*/
public native ByteBuffer containerId();
/**
* Returns a dump of this descriptor.
*
*
* @return The descriptor dump.
*/
public String dump()
{
return String.format("Container Id Descriptor:%n"
+ " bLength %18d%n"
+ " bDescriptorType %10d%n"
+ " bDevCapabilityType %7d%n"
+ " bReserved %16d%n"
+ " containerId:%n%s%n",
bLength() & 0xff,
bDescriptorType() & 0xff,
bDevCapabilityType() & 0xff,
bReserved() & 0xff,
DescriptorUtils.dump(containerId()).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 ContainerIdDescriptor other =
(ContainerIdDescriptor) obj;
return new EqualsBuilder()
.append(bDescriptorType(), other.bDescriptorType())
.append(bLength(), other.bLength())
.append(bDevCapabilityType(), other.bDevCapabilityType())
.append(bReserved(), other.bReserved())
.append(containerId().array(), other.containerId().array())
.isEquals();
return String.format(
"Container ID Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" bDevCapabilityType %7d%n" +
" bReserved %16d%n" +
" ContainerID:%n%s%n",
this.bLength() & 0xFF,
this.bDescriptorType() & 0xFF,
this.bDevCapabilityType() & 0xFF,
this.bReserved() & 0xFF,
DescriptorUtils.dump(this.containerId())
.replaceAll("(?m)^", " "));
}
@Override
public int hashCode()
{
return new HashCodeBuilder()
.append(bLength())
.append(bDescriptorType())
.append(bDevCapabilityType())
.append(bReserved())
.append(containerId())
.append(this.bLength())
.append(this.bDescriptorType())
.append(this.bDevCapabilityType())
.append(this.bReserved())
.append(this.containerId())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (obj == null)
{
return false;
}
if (obj == this)
{
return true;
}
if (obj.getClass() != this.getClass())
{
return false;
}
final ContainerIdDescriptor other = (ContainerIdDescriptor) obj;
return new EqualsBuilder()
.append(this.bLength(), other.bLength())
.append(this.bDescriptorType(), other.bDescriptorType())
.append(this.bDevCapabilityType(), other.bDevCapabilityType())
.append(this.bReserved(), other.bReserved())
.append(this.containerId(), other.containerId())
.isEquals();
}
@Override
public String toString()
{
return dump();
return this.dump();
}
}

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -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
@ -28,51 +26,75 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
* {@link LibUsb#setDebug(Context, int)} will not affect the other user of the
* library, and {@link LibUsb#exit(Context)} will not destroy resources that the
* other user is still using.
*
*
* Sessions are created by {@link LibUsb#init(Context)} and destroyed through
* {@link LibUsb#exit(Context)}. If your application is guaranteed to only ever
* include a single libusb user (i.e. you), you do not have to worry about
* contexts: pass NULL in every function call where a context is required. The
* default context will be used.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
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
}
/**
* Returns the native pointer to the context structure.
*
*
* @return The native pointer to the context structure.
*/
public long getPointer()
{
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) (this.contextPointer ^ (this.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 (this.getClass() != obj.getClass())
{
return false;
}
final Context other = (Context) obj;
return this.contextPointer == other.contextPointer;
}
if (this.contextPointer != other.contextPointer)
{
return false;
}
return true;
}
@Override
public String toString()
{
return String.format("libusb context 0x%x", this.contextPointer);
}
}

View File

@ -0,0 +1,150 @@
/*
* Copyright 2013 Luca Longinotti <l@longi.li>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
* Copyright 2008-2011 Nathan Hjelm <hjelmn@users.sourceforge.net>
* Copyright 2009-2012 Pete Batard <pete@akeo.ie>
* Copyright 2009-2012 Ludovic Rousseau <ludovic.rousseau@gmail.com>
* Copyright 2010-2012 Michael Plante <michael.plante@gmail.com>
* Copyright 2011-2012 Hans de Goede <hdegoede@redhat.com>
* Copyright 2012 Martin Pieuchot <mpi@openbsd.org>
* Copyright 2012-2013 Toby Gray <toby.gray@realvnc.com>
*/
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");
}
this.controlSetup = BufferUtils.slice(buffer, 0,
LibUsb.CONTROL_SETUP_SIZE);
// Control Setup (as all of USB) is Little Endian.
this.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 this.controlSetup.get(0);
}
public void setBmRequestType(final byte bmRequestType)
{
this.controlSetup.put(0, bmRequestType);
}
public byte bRequest()
{
return this.controlSetup.get(1);
}
public void setBRequest(final byte bRequest)
{
this.controlSetup.put(1, bRequest);
}
public short wValue()
{
return this.controlSetup.getShort(2);
}
public void setWValue(final short wValue)
{
this.controlSetup.putShort(2, wValue);
}
public short wIndex()
{
return this.controlSetup.getShort(4);
}
public void setWIndex(final short wIndex)
{
this.controlSetup.putShort(4, wIndex);
}
public short wLength()
{
return this.controlSetup.getShort(6);
}
public void setWLength(final short wLength)
{
this.controlSetup.putShort(6, wLength);
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = (prime * result)
+ ((this.controlSetup == null) ? 0 : this.controlSetup.hashCode());
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (this.getClass() != obj.getClass())
{
return false;
}
final ControlSetup other = (ControlSetup) obj;
if (this.controlSetup == null)
{
if (other.controlSetup != null)
{
return false;
}
}
else if (!this.controlSetup.equals(other.controlSetup))
{
return false;
}
return true;
}
@Override
public String toString()
{
return String.format("libusb control setup with buffer %s",
this.controlSetup.toString());
}
}

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -18,18 +18,16 @@
package de.ailis.usb4java.libusb;
import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* Structure representing a USB device detected on the system.
*
*
* This is an opaque type for which you are only ever provided with a pointer,
* usually originating from {@link LibUsb#getDeviceList(Context, DeviceList)}.
*
*
* Certain operations can be performed on a device, but in order to do any I/O
* you will have to first obtain a device handle using
* {@link LibUsb#open(Device, DeviceHandle)}.
*
*
* Devices are reference counted with {@link LibUsb#refDevice(Device)} and
* {@link LibUsb#unrefDevice(Device)}, and are freed when the reference count
* reaches 0. New devices presented by
@ -38,7 +36,7 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
* decrease the reference count on all devices in the list.
* {@link LibUsb#open(Device, DeviceHandle)} adds another reference which is
* later destroyed by {@link LibUsb#close(DeviceHandle)}.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class Device
@ -54,30 +52,48 @@ public final class Device
{
// Empty
}
/**
* Returns the native pointer to the device structure.
*
*
* @return The native pointer to the device structure.
*/
public long getPointer()
{
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) (this.devicePointer ^ (this.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 (this.getClass() != obj.getClass())
{
return false;
}
final Device other = (Device) obj;
return this.devicePointer == other.devicePointer;
if (this.devicePointer != other.devicePointer)
{
return false;
}
return true;
}
@Override

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -18,8 +18,6 @@
package de.ailis.usb4java.libusb;
import java.nio.ByteBuffer;
import javax.usb.UsbDeviceDescriptor;
import org.apache.commons.lang3.builder.EqualsBuilder;
@ -29,16 +27,16 @@ import de.ailis.usb4java.utils.DescriptorUtils;
/**
* A structure representing the standard USB device descriptor.
*
*
* This descriptor is documented in section 9.6.1 of the USB 3.0 specification.
* All multiple-byte fields are represented in host-endian format.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
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.
*
* @return The native data.
* Returns the native pointer.
*
* @return The native pointer.
*/
public ByteBuffer getData()
public long getPointer()
{
return this.deviceDescriptorData;
return this.deviceDescriptorPointer;
}
@Override
@ -103,17 +101,17 @@ public final class DeviceDescriptor implements UsbDeviceDescriptor
/**
* Returns a dump of this descriptor.
*
*
* @return The descriptor dump.
*/
public String dump()
{
return dump(null);
return this.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.
@ -121,64 +119,76 @@ public final class DeviceDescriptor implements UsbDeviceDescriptor
*/
public String dump(final DeviceHandle handle)
{
final String sManufacturer = LibUsb.getStringDescriptor(handle,
iManufacturer());
final String sProduct = LibUsb.getStringDescriptor(handle, iProduct());
final String sSerialNumber = LibUsb.getStringDescriptor(handle,
iSerialNumber());
return DescriptorUtils.dump(this, sManufacturer, sProduct,
final String sManufacturer = LibUsb.getStringDescriptor(handle,
this.iManufacturer());
final String sProduct = LibUsb.getStringDescriptor(handle,
this.iProduct());
final String sSerialNumber = LibUsb.getStringDescriptor(handle,
this.iSerialNumber());
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()
{
return new HashCodeBuilder()
.append(bLength())
.append(bDescriptorType())
.append(bcdUSB())
.append(bDeviceClass())
.append(bDeviceSubClass())
.append(bDeviceProtocol())
.append(bMaxPacketSize0())
.append(idVendor())
.append(idProduct())
.append(bcdDevice())
.append(iManufacturer())
.append(iProduct())
.append(iSerialNumber())
.append(bNumConfigurations()).toHashCode();
.append(this.bLength())
.append(this.bDescriptorType())
.append(this.bcdUSB())
.append(this.bDeviceClass())
.append(this.bDeviceSubClass())
.append(this.bDeviceProtocol())
.append(this.bMaxPacketSize0())
.append(this.idVendor())
.append(this.idProduct())
.append(this.bcdDevice())
.append(this.iManufacturer())
.append(this.iProduct())
.append(this.iSerialNumber())
.append(this.bNumConfigurations())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (this.getClass() != obj.getClass())
{
return false;
}
final DeviceDescriptor other = (DeviceDescriptor) obj;
return new EqualsBuilder()
.append(this.bLength(), other.bLength())
.append(this.bDescriptorType(), other.bDescriptorType())
.append(this.bcdUSB(), other.bcdUSB())
.append(this.bDeviceClass(), other.bDeviceClass())
.append(this.bDeviceSubClass(), other.bDeviceSubClass())
.append(this.bDeviceProtocol(), other.bDeviceProtocol())
.append(this.bMaxPacketSize0(), other.bMaxPacketSize0())
.append(this.idVendor(), other.idVendor())
.append(this.idProduct(), other.idProduct())
.append(this.bcdDevice(), other.bcdDevice())
.append(this.iManufacturer(), other.iManufacturer())
.append(this.iProduct(), other.iProduct())
.append(this.iSerialNumber(), other.iSerialNumber())
.append(this.bNumConfigurations(), other.bNumConfigurations())
.isEquals();
}
@Override
public String toString()
{
return dump();
return this.dump();
}
}

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -18,17 +18,15 @@
package de.ailis.usb4java.libusb;
import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* Structure representing a handle on a USB device.
*
*
* This is an opaque type for which you are only ever provided with a pointer,
* usually originating from {@link LibUsb#open(Device, DeviceHandle)}.
*
*
* A device handle is used to perform I/O and other operations. When finished
* with a device handle, you should call {@link LibUsb#close(DeviceHandle)}.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class DeviceHandle
@ -48,7 +46,7 @@ public final class DeviceHandle
/**
* Returns the native pointer to the device handle structure.
*
*
* @return The native pointer to the device handle structure.
*/
public long getPointer()
@ -59,22 +57,40 @@ 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) (this.deviceHandlePointer ^ (this.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 (this.getClass() != obj.getClass())
{
return false;
}
final DeviceHandle other = (DeviceHandle) obj;
return this.deviceHandlePointer == other.deviceHandlePointer;
if (this.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",
this.deviceHandlePointer);
}
}

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -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,21 +41,21 @@ public final class DeviceList implements Iterable<Device>
public DeviceList()
{
// Empty
}
}
/**
* Returns the native pointer.
*
*
* @return The native pointer.
*/
public long getPointer()
{
return this.deviceListPointer;
}
/**
* Returns the number of devices in the list.
*
*
* @return The number of devices in the list.
*/
public int getSize()
@ -67,7 +65,7 @@ public final class DeviceList implements Iterable<Device>
/**
* Returns the device with the specified index.
*
*
* @param index
* The device index.
* @return The device or null when index is out of bounds.
@ -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) (this.deviceListPointer ^ (this.deviceListPointer >>> 32));
result = (prime * result) + this.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 (this.getClass() != obj.getClass())
{
return false;
}
final DeviceList other = (DeviceList) obj;
return this.deviceListPointer == other.deviceListPointer;
if (this.deviceListPointer != other.deviceListPointer)
{
return false;
}
if (this.size != other.size)
{
return false;
}
return true;
}
@Override
public String toString()
{
return String.format("libusb device list 0x%x with size %d",
this.deviceListPointer, this.size);
}
}

View File

@ -9,7 +9,7 @@ import java.util.Iterator;
/**
* Iterator for device list.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
final class DeviceListIterator implements Iterator<Device>
@ -22,7 +22,7 @@ final class DeviceListIterator implements Iterator<Device>
/**
* Constructor.
*
*
* @param devices
* The devices list.
*/

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -29,7 +29,7 @@ import de.ailis.usb4java.utils.DescriptorUtils;
/**
* A structure representing the standard USB endpoint descriptor.
*
*
* This descriptor is documented in section 9.6.6 of the USB 3.0 specification.
* All multiple-byte fields are represented in host-endian format.
*
@ -41,17 +41,17 @@ 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.
*
*
* @return The native pointer.
*/
public long getPointer()
@ -80,91 +80,104 @@ public final class EndpointDescriptor implements UsbEndpointDescriptor
/**
* For audio devices only: the rate at which synchronization feedback is
* provided.
*
*
* @return The synchronization feedback rate.
*/
public native byte bRefresh();
/**
* For audio devices only: the address of the synch endpoint.
*
*
* @return The synch endpoint address.
*/
public native byte bSynchAddress();
/**
* Extra descriptors.
*
*
* If libusb encounters unknown endpoint descriptors, it will store them
* here, should you wish to parse them.
*
*
* @return The extra descriptors.
*/
public native ByteBuffer extra();
/**
* Length of the extra descriptors, in bytes.
*
*
* @return The extra descriptors length.
*/
public native int extraLength();
/**
* Returns a dump of this descriptor.
*
*
* @return The descriptor dump.
*/
public String dump()
{
return String.format("%s"
+ " extralen %17d%n"
+ " extra:%n"
+ "%s%n",
return String.format(
"%s%n" +
" extralen %17d%n" +
" extra:%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();
this.extraLength(),
DescriptorUtils.dump(this.extra()).replaceAll("(?m)^", " "));
}
@Override
public int hashCode()
{
return new HashCodeBuilder()
.append(bLength())
.append(bDescriptorType())
.append(bEndpointAddress())
.append(bmAttributes())
.append(wMaxPacketSize())
.append(bInterval())
.append(bRefresh())
.append(bSynchAddress())
.append(extra())
.append(extraLength())
.append(this.bLength())
.append(this.bDescriptorType())
.append(this.bEndpointAddress())
.append(this.bmAttributes())
.append(this.wMaxPacketSize())
.append(this.bInterval())
.append(this.bRefresh())
.append(this.bSynchAddress())
.append(this.extra())
.append(this.extraLength())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (this.getClass() != obj.getClass())
{
return false;
}
final EndpointDescriptor other = (EndpointDescriptor) obj;
return new EqualsBuilder()
.append(this.bLength(), other.bLength())
.append(this.bDescriptorType(), other.bDescriptorType())
.append(this.bEndpointAddress(), other.bEndpointAddress())
.append(this.bmAttributes(), other.bmAttributes())
.append(this.wMaxPacketSize(), other.wMaxPacketSize())
.append(this.bInterval(), other.bInterval())
.append(this.bRefresh(), other.bRefresh())
.append(this.bSynchAddress(), other.bSynchAddress())
.append(this.extra(), other.extra())
.append(this.extraLength(), other.extraLength())
.isEquals();
}
@Override
public String toString()
{
return dump();
return this.dump();
}
}

View File

@ -0,0 +1,11 @@
/*
* Copyright 2013 Luca Longinotti <l@longi.li>
* See LICENSE.md for licensing information.
*/
package de.ailis.usb4java.libusb;
public interface HotplugCallback
{
int processEvent(Context context, Device device, int event, Object userData);
}

View File

@ -0,0 +1,98 @@
/*
* Copyright 2013 Luca Longinotti <l@longi.li>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
* Copyright 2008-2011 Nathan Hjelm <hjelmn@users.sourceforge.net>
* Copyright 2009-2012 Pete Batard <pete@akeo.ie>
* Copyright 2009-2012 Ludovic Rousseau <ludovic.rousseau@gmail.com>
* Copyright 2010-2012 Michael Plante <michael.plante@gmail.com>
* Copyright 2011-2012 Hans de Goede <hdegoede@redhat.com>
* Copyright 2012 Martin Pieuchot <mpi@openbsd.org>
* Copyright 2012-2013 Toby Gray <toby.gray@realvnc.com>
*/
package de.ailis.usb4java.libusb;
/**
* Hotplug Callback Handle.
*
* Callback handles are generated by {@link LibUsb#hotplugRegisterCallback(Context,
* int, int, short, short, byte, HotplugCallback, Object, HotplugCallbackHandle)}
* and can be used to deregister callbacks. Callback handles are unique
* per {@link Context} and it is safe to call
* {@link LibUsb#hotplugDeregisterCallback(Context, HotplugCallbackHandle)}
* on an already deregistered callback.
*
* @author Luca Longinotti (l@longi.li)
*/
public final class HotplugCallbackHandle
{
/** The hotplug callback handle, it's an integer (int) in C. */
private long hotplugCallbackHandleValue;
/**
* Constructs a new hotplug callback handle. Must be passed to
* {@link LibUsb#hotplugRegisterCallback(Context, int, int, short, short,
* byte, HotplugCallback, Object, HotplugCallbackHandle)} before passing it
* to any other method.
*/
public HotplugCallbackHandle()
{
// Empty
}
/**
* Returns the hotplug callback handle value.
*
* @return The hotplug callback handle value.
*/
public long getValue()
{
return this.hotplugCallbackHandleValue;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = (prime * result)
+ (int) (this.hotplugCallbackHandleValue ^ (this.hotplugCallbackHandleValue >>> 32));
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (this.getClass() != obj.getClass())
{
return false;
}
final HotplugCallbackHandle other = (HotplugCallbackHandle) obj;
if (this.hotplugCallbackHandleValue != other.hotplugCallbackHandleValue)
{
return false;
}
return true;
}
@Override
public String toString()
{
return String.format("libusb hotplug callback handle 0x%x",
this.hotplugCallbackHandleValue);
}
}

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -18,11 +18,12 @@
package de.ailis.usb4java.libusb;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* A collection of alternate settings for a particular USB interface.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class Interface
@ -31,35 +32,35 @@ 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.
*
*
* @return The native pointer.
*/
public long getPointer()
{
return this.interfacePointer;
}
/**
* Returns the array with interface descriptors. The length of this array is
* determined by the {@link #numAltsetting()} field.
*
*
* @return The array with interface descriptors.
*/
public native InterfaceDescriptor[] altsetting();
/**
* Returns the number of alternate settings that belong to this interface.
*
*
* @return The number of alternate settings.
*/
public native int numAltsetting();
@ -70,46 +71,58 @@ 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",
this.numAltsetting()));
for (final InterfaceDescriptor intDesc : this.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(this.altsetting())
.append(this.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 (this.getClass() != obj.getClass())
{
return false;
}
final Interface other = (Interface) obj;
return this.interfacePointer == other.interfacePointer;
return new EqualsBuilder()
.append(this.altsetting(), other.altsetting())
.append(this.numAltsetting(), other.numAltsetting())
.isEquals();
}
@Override
public String toString()
{
return dump();
return this.dump();
}
}

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -29,10 +29,10 @@ import de.ailis.usb4java.utils.DescriptorUtils;
/**
* A structure representing the standard USB interface descriptor.
*
*
* This descriptor is documented in section 9.6.5 of the USB 3.0 specification.
* All multiple-byte fields are represented in host-endian format.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class InterfaceDescriptor implements UsbInterfaceDescriptor
@ -41,17 +41,17 @@ 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.
*
*
* @return The native pointer.
*/
public long getPointer()
@ -88,110 +88,110 @@ public final class InterfaceDescriptor implements UsbInterfaceDescriptor
/**
* Returns the array with endpoints.
*
*
* @return The array with endpoints.
*/
public native EndpointDescriptor[] endpoint();
/**
* Extra descriptors.
*
*
* If libusb encounters unknown interface descriptors, it will store them
* here, should you wish to parse them.
*
*
* @return The extra descriptors.
*/
public native ByteBuffer extra();
/**
* Length of the extra descriptors, in bytes.
*
*
* @return The extra descriptors length.
*/
public native int extraLength();
/**
* Returns a dump of this descriptor.
*
*
* @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),
this.extraLength(),
DescriptorUtils.dump(this.extra()).replaceAll("(?m)^", " ")));
for (final EndpointDescriptor epDesc : this.endpoint())
{
builder.append("%n" + epDesc.dump());
}
return builder.toString();
}
@Override
public int hashCode()
{
return new HashCodeBuilder()
.append(bLength())
.append(bDescriptorType())
.append(bInterfaceNumber())
.append(bAlternateSetting())
.append(bNumEndpoints())
.append(bInterfaceClass())
.append(bInterfaceSubClass())
.append(bInterfaceProtocol())
.append(iInterface())
.append(extra())
.append(extraLength())
.append(this.bLength())
.append(this.bDescriptorType())
.append(this.bInterfaceNumber())
.append(this.bAlternateSetting())
.append(this.bNumEndpoints())
.append(this.bInterfaceClass())
.append(this.bInterfaceSubClass())
.append(this.bInterfaceProtocol())
.append(this.iInterface())
.append(this.endpoint())
.append(this.extra())
.append(this.extraLength())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (this.getClass() != obj.getClass())
{
return false;
}
final InterfaceDescriptor other = (InterfaceDescriptor) obj;
return new EqualsBuilder()
.append(this.bLength(), other.bLength())
.append(this.bDescriptorType(), other.bDescriptorType())
.append(this.bInterfaceNumber(), other.bInterfaceNumber())
.append(this.bAlternateSetting(), other.bAlternateSetting())
.append(this.bNumEndpoints(), other.bNumEndpoints())
.append(this.bInterfaceClass(), other.bInterfaceClass())
.append(this.bInterfaceSubClass(), other.bInterfaceSubClass())
.append(this.bInterfaceProtocol(), other.bInterfaceProtocol())
.append(this.iInterface(), other.iInterface())
.append(this.endpoint(), other.endpoint())
.append(this.extra(), other.extra())
.append(this.extraLength(), other.extraLength())
.isEquals();
}
@Override
public String toString()
{
return dump();
return this.dump();
}
}

View File

@ -0,0 +1,95 @@
/*
* Copyright 2013 Luca Longinotti <l@longi.li>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
* Copyright 2008-2011 Nathan Hjelm <hjelmn@users.sourceforge.net>
* Copyright 2009-2012 Pete Batard <pete@akeo.ie>
* Copyright 2009-2012 Ludovic Rousseau <ludovic.rousseau@gmail.com>
* Copyright 2010-2012 Michael Plante <michael.plante@gmail.com>
* Copyright 2011-2012 Hans de Goede <hdegoede@redhat.com>
* Copyright 2012 Martin Pieuchot <mpi@openbsd.org>
* Copyright 2012-2013 Toby Gray <toby.gray@realvnc.com>
*/
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 this.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) (this.isoPacketDescriptorPointer ^ (this.isoPacketDescriptorPointer >>> 32));
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (this.getClass() != obj.getClass())
{
return false;
}
final IsoPacketDescriptor other = (IsoPacketDescriptor) obj;
if (this.isoPacketDescriptorPointer != other.isoPacketDescriptorPointer)
{
return false;
}
return true;
}
@Override
public String toString()
{
return String.format("libusb iso packet descriptor 0x%x",
this.isoPacketDescriptorPointer);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>

View File

@ -14,7 +14,7 @@ import java.net.URL;
/**
* Utility class to load native libraries from classpath.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class Loader
@ -80,15 +80,21 @@ public final class Loader
* "osx" or (for any other non-supported platform) the value of the
* "os.name" property converted to lower case and with removed space
* characters.
*
*
* @return The operating system name.
*/
private static String getOS()
{
final String os = System.getProperty("os.name").toLowerCase()
.replace(" ", "");
if (os.contains(OS_WINDOWS)) return OS_WINDOWS;
if (os.equals(OS_MACOSX)) return OS_OSX;
if (os.contains(OS_WINDOWS))
{
return OS_WINDOWS;
}
if (os.equals(OS_MACOSX))
{
return OS_OSX;
}
return os;
}
@ -97,20 +103,27 @@ public final class Loader
* names i386 und amd64 are converted accordingly) or (when platform is
* unsupported) the value of os.arch converted to lower-case and with
* removed space characters.
*
*
* @return The CPU architecture
*/
private static String getArch()
{
final String arch = System.getProperty("os.arch");
if (arch.equals(ARCH_I386)) return ARCH_X86;
if (arch.equals(ARCH_AMD64)) return ARCH_X86_64;
return arch.toLowerCase().replace(" ", "");
final String arch = System.getProperty("os.arch").toLowerCase()
.replace(" ", "");
if (arch.equals(ARCH_I386))
{
return ARCH_X86;
}
if (arch.equals(ARCH_AMD64))
{
return ARCH_X86_64;
}
return arch;
}
/**
* Returns the shared library extension name.
*
*
* @return The shared library extension name.
*/
private static String getExt()
@ -118,51 +131,67 @@ public final class Loader
final String os = getOS();
final String key = "usb4java.libext." + getOS();
final String ext = System.getProperty(key);
if (ext != null) return ext;
if (ext != null)
{
return ext;
}
if (os.equals(OS_LINUX) || os.equals(OS_FREEBSD) || os.equals(OS_SUNOS))
{
return EXT_SO;
}
if (os.equals(OS_WINDOWS))
{
return EXT_DLL;
}
if (os.equals(OS_OSX))
{
return EXT_DYLIB;
throw new LoaderException("Unable to determine the shared library " +
"file extension for operating system '" + os +
"'. Please specify Java parameter -D" + key + "=<FILE-EXTENSION>");
}
throw new LoaderException("Unable to determine the shared library "
+ "file extension for operating system '" + os
+ "'. Please specify Java parameter -D" + key + "=<FILE-EXTENSION>");
}
/**
* Creates the temporary directory used for unpacking the native libraries.
* This directory is marked for deletion on exit.
*
*
* @return The temporary directory for native libraries.
*/
private static File createTempDirectory()
{
// Return cached tmp directory when already created
if (tmp != null) return tmp;
if (tmp != null)
{
return tmp;
}
try
{
tmp = File.createTempFile("usb4java", null);
if (!tmp.delete())
{
throw new IOException("Unable to delete temporary file " + tmp);
}
if (!tmp.mkdirs())
{
throw new IOException("Unable to create temporary directory "
+ tmp);
}
tmp.deleteOnExit();
return tmp;
}
catch (final IOException e)
{
throw new LoaderException("Unable to create temporary directory " +
"for usb4java natives: " + e, e);
throw new LoaderException("Unable to create temporary directory "
+ "for usb4java natives: " + e, e);
}
}
/**
* Returns the platform name. This could be for example "linux-x86" or
* "windows-x86_64".
*
*
* @return The architecture name. Never null.
*/
private static String getPlatform()
@ -173,7 +202,7 @@ public final class Loader
/**
* Returns the name of the usb4java native library. This could be
* "libusb4java.dll" for example.
*
*
* @return The usb4java native library name. Never null.
*/
private static String getLibName()
@ -185,19 +214,22 @@ public final class Loader
* Returns the name of the libusb native library. This could be
* "libusb0.dll" for example or null if this library is not needed on the
* current platform (Because it is provided by the operating system).
*
*
* @return The libusb native library name or null if not needed.
*/
private static String getExtraLibName()
{
final String os = getOS();
if (os.equals(OS_WINDOWS)) return "libusb-1.0." + EXT_DLL;
if (os.equals(OS_WINDOWS))
{
return "libusb-1.0." + EXT_DLL;
}
return null;
}
/**
* Copies the specified input stream to the specified output file.
*
*
* @param input
* The input stream.
* @param output
@ -226,25 +258,27 @@ public final class Loader
/**
* Extracts a single library.
*
*
* @param platform
* The platform name (For example "linux-x86")
* @param lib
* The library name to extract (For example "libusb0.dll")
* @return The absolute path to the extracted library.
*/
private static String extractLibrary(final String platform,
final String lib)
private static String extractLibrary(final String platform, final String lib)
{
// Extract the usb4java library
final String source = '/' +
Loader.class.getPackage().getName().replace('.', '/') +
'/' + platform + "/" + lib;
final String source = '/'
+ Loader.class.getPackage().getName().replace('.', '/') + '/'
+ platform + "/" + lib;
// Check if native library is present
final URL url = Loader.class.getResource(source);
if (url == null) throw new LoaderException(
"Native library not found in classpath: " + source);
if (url == null)
{
throw new LoaderException("Native library not found in classpath: "
+ source);
}
// If native library was found in an already extracted form then
// return this one without extracting it
@ -267,11 +301,12 @@ public final class Loader
final File dest = new File(createTempDirectory(), lib);
try
{
final InputStream stream =
Loader.class.getResourceAsStream(source);
final InputStream stream = Loader.class.getResourceAsStream(source);
if (stream == null)
{
throw new LoaderException("Unable to find " + source
+ " in the classpath");
}
try
{
copy(stream, dest);
@ -283,9 +318,8 @@ public final class Loader
}
catch (final IOException e)
{
throw new LoaderException(
"Unable to extract native library " + source + " to " + dest
+ ": " + e, e);
throw new LoaderException("Unable to extract native library "
+ source + " to " + dest + ": " + e, e);
}
// Mark usb4java library for deletion
@ -299,19 +333,24 @@ public final class Loader
* times. Duplicate calls are ignored. This method is automatically called
* when the {@link LibUsb} class is loaded. When you need to do it earlier
* (To catch exceptions for example) then simply call this method manually.
*
*
* @throws LoaderException
* When loading the native wrapper libraries failed.
*/
public static void load()
{
if (loaded) return;
if (loaded)
{
return;
}
final String platform = getPlatform();
final String lib = getLibName();
final String extraLib = getExtraLibName();
if (extraLib != null)
{
System.load(extractLibrary(platform, extraLib));
}
System.load(extractLibrary(platform, lib));
loaded = true;
}

View File

@ -7,7 +7,7 @@ package de.ailis.usb4java.libusb;
/**
* Thrown when JNI library could not be loaded.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class LoaderException extends RuntimeException
@ -17,7 +17,7 @@ public final class LoaderException extends RuntimeException
/**
* Constructor.
*
*
* @param message
* The error message.
*/
@ -28,7 +28,7 @@ public final class LoaderException extends RuntimeException
/**
* Constructor.
*
*
* @param message
* The error message.
* @param cause

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -22,7 +22,7 @@ import java.io.FileDescriptor;
/**
* Listener interface for pollfd notifications.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public interface PollfdListener
@ -30,7 +30,7 @@ public interface PollfdListener
/**
* Callback function, invoked when a new file descriptor should be added to
* the set of file descriptors monitored for events.
*
*
* @param fd
* the new file descriptor.
* @param events
@ -43,10 +43,10 @@ public interface PollfdListener
/**
* Callback function, invoked when a file descriptor should be removed from
* the set of file descriptors being monitored for events.
*
*
* After returning from this callback, do not use that file descriptor
* again.
*
*
* @param fd
* The file descriptor to stop monitoring.
* @param userData

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -23,21 +23,20 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* A structure representing the superspeed endpoint companion descriptor.
*
*
* This descriptor is documented in section 9.6.7 of the USB 3.0 specification.
* All multiple-byte fields are represented in host-endian format.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class SsEndpointCompanionDescriptor
{
/** The native pointer to the descriptor structure. */
private long ssEndpointCompanionDescriptor;
private long ssEndpointCompanionDescriptorPointer;
/**
* Constructs a new descriptor which can be passed to the
* {@link LibUsb#getSsEndpointCompanionDescriptor(Context,
* EndpointDescriptor, SsEndpointCompanionDescriptor)}
* {@link LibUsb#getSsEndpointCompanionDescriptor(Context, EndpointDescriptor, SsEndpointCompanionDescriptor)}
* method.
*/
public SsEndpointCompanionDescriptor()
@ -47,24 +46,24 @@ public final class SsEndpointCompanionDescriptor
/**
* Returns the native pointer.
*
*
* @return The native pointer.
*/
public long getPointer()
{
return this.ssEndpointCompanionDescriptor;
return this.ssEndpointCompanionDescriptorPointer;
}
/**
* Returns the size of this descriptor (in bytes).
*
*
* @return The descriptor size in bytes;
*/
public native byte bLength();
/**
* Returns the descriptor type.
*
*
* @return The descriptor type.
*/
public native byte bDescriptorType();
@ -72,17 +71,17 @@ public final class SsEndpointCompanionDescriptor
/**
* Returns the maximum number of packets the endpoint can send or receive as
* part of a burst.
*
*
* @return The maximum number of packets as part of a burst.
*/
public native byte bMaxBurst();
/**
* Returns the attributes. In bulk endpoint: bits 4:0 represents the
* maximum number of streams the EP supports. In isochronous endpoint:
* bits 1:0 represents the Mult - a zero based value that determines the
* Returns the attributes. In bulk endpoint: bits 4:0 represents the
* maximum number of streams the EP supports. In isochronous endpoint:
* bits 1:0 represents the Mult - a zero based value that determines the
* maximum number of packets within a service interval.
*
*
* @return The attributes.
*/
public native byte bmAttributes();
@ -90,62 +89,74 @@ public final class SsEndpointCompanionDescriptor
/**
* Returns the total number of bytes this endpoint will transfer every
* service interval. Valid only for periodic endpoints.
*
*
* @return The total number of bytes per service interval.
*/
public native short wBytesPerInterval();
/**
* Returns a dump of this descriptor.
*
*
* @return The descriptor dump.
*/
public String dump()
{
return String.format("Device Descriptor:%n"
+ " bLength %18d%n"
+ " bDescriptorType %10d%n"
+ " bMaxBurst %16s%n"
+ " bmAttributes %13d%n"
+ " wBytesPerInterval %8d%n",
bLength() & 0xff,
bDescriptorType() & 0xff,
bMaxBurst() & 0xff,
bmAttributes() & 0xff,
wBytesPerInterval() & 0xffff);
}
@Override
public boolean equals(final Object obj)
{
if (obj == null) return false;
if (obj == this) return true;
if (obj.getClass() != getClass()) return false;
final SsEndpointCompanionDescriptor other =
(SsEndpointCompanionDescriptor) obj;
return new EqualsBuilder()
.append(bDescriptorType(), other.bDescriptorType())
.append(bLength(), other.bLength())
.append(bMaxBurst(), other.bMaxBurst())
.append(bmAttributes(), other.bmAttributes())
.append(wBytesPerInterval(), other.wBytesPerInterval()).isEquals();
return String.format(
"SuperSpeed Endpoint Companion Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" bMaxBurst %16s%n" +
" bmAttributes %13d%n" +
" wBytesPerInterval %8d%n",
this.bLength() & 0xFF,
this.bDescriptorType() & 0xFF,
this.bMaxBurst() & 0xFF,
this.bmAttributes() & 0xFF,
this.wBytesPerInterval() & 0xFFFF);
}
@Override
public int hashCode()
{
return new HashCodeBuilder()
.append(bLength())
.append(bDescriptorType())
.append(bMaxBurst())
.append(bmAttributes())
.append(wBytesPerInterval())
.append(this.bLength())
.append(this.bDescriptorType())
.append(this.bMaxBurst())
.append(this.bmAttributes())
.append(this.wBytesPerInterval())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (obj == null)
{
return false;
}
if (obj == this)
{
return true;
}
if (obj.getClass() != this.getClass())
{
return false;
}
final SsEndpointCompanionDescriptor other = (SsEndpointCompanionDescriptor) obj;
return new EqualsBuilder()
.append(this.bLength(), other.bLength())
.append(this.bDescriptorType(), other.bDescriptorType())
.append(this.bMaxBurst(), other.bMaxBurst())
.append(this.bmAttributes(), other.bmAttributes())
.append(this.wBytesPerInterval(), other.wBytesPerInterval())
.isEquals();
}
@Override
public String toString()
{
return dump();
return this.dump();
}
}

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -23,11 +23,11 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* A structure representing the SuperSpeed USB Device Capability descriptor.
* This descriptor is documented in section 9.6.2.2 of the USB 3.0
* This descriptor is documented in section 9.6.2.2 of the USB 3.0
* specification.
*
*
* All multiple-byte fields are represented in host-endian format.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class SsUsbDeviceCapabilityDescriptor
@ -36,10 +36,10 @@ public final class SsUsbDeviceCapabilityDescriptor
private long ssUsbDeviceCapabilityDescriptorPointer;
/**
* Constructs a new SuperSpeed USB Device Capability descriptor which can
* Constructs a new SuperSpeed USB Device Capability descriptor which can
* be passed to the
* {@link LibUsb#getSsUsbDeviceCapabilityDescriptor(Context,
* BosDevCapabilityDescriptor, SsUsbDeviceCapabilityDescriptor)} method.
* {@link LibUsb#getSsUsbDeviceCapabilityDescriptor(Context, BosDevCapabilityDescriptor, SsUsbDeviceCapabilityDescriptor)}
* method.
*/
public SsUsbDeviceCapabilityDescriptor()
{
@ -48,7 +48,7 @@ public final class SsUsbDeviceCapabilityDescriptor
/**
* Returns the native pointer.
*
*
* @return The native pointer.
*/
public long getPointer()
@ -58,126 +58,137 @@ public final class SsUsbDeviceCapabilityDescriptor
/**
* Returns the size of this descriptor (in bytes).
*
*
* @return The descriptor size in bytes;
*/
public native byte bLength();
/**
* Returns the descriptor type.
*
*
* @return The descriptor type.
*/
public native byte bDescriptorType();
/**
* Returns the device capability type.
*
*
* @return The device capability type.
*/
public native byte bDevCapabilityType();
/**
* Returns the bitmap of supported device level features.
*
* Returns the bitmap of supported device level features.
*
* @return The supported device level features.
*/
public native byte bmAttributes();
/**
* Returns the bitmap encoding of the speed supported by this device when
* Returns the bitmap encoding of the speed supported by this device when
* operating in SuperSpeed mode.
*
*
* @return The supported speed.
*/
public native short wSpeedSupported();
/**
* Returns the lowest speed at which all the functionality supported by the
* Returns the lowest speed at which all the functionality supported by the
* device is available to the user.
*
*
* @return The lowest speed.
*/
public native byte bFunctionalitySupport();
/**
* Returns the U1 Device Exit Latency.
*
*
* @return The U1 Device Exit Latency.
*/
public native byte bU1DevExitLat();
/**
* Returns the U2 Device Exit Latency.
*
*
* @return The U2 Device Exit Latency.
*/
public native short bU2DevExitLat();
/**
* Returns a dump of this descriptor.
*
*
* @return The descriptor dump.
*/
public String dump()
{
return String.format("SuperSpeed USB Device Capability descriptor:%n"
+ " bLength %18d%n"
+ " bDescriptorType %10d%n"
+ " bDevCapabilityType %7d%n"
+ " bmAttributes %13s%n"
+ " wSpeedSupported %10d%n"
+ " bFunctionalitySupport %4d%n"
+ " bU1DevExitLat %12d%n"
+ " bU2DevExitLat %12d%n",
bLength() & 0xff,
bDescriptorType() & 0xff,
bDevCapabilityType() & 0xff,
String.format("0x%02x", bmAttributes() & 0xff),
wSpeedSupported() & 0xffff,
bFunctionalitySupport() & 0xff,
bU1DevExitLat() & 0xff,
bU2DevExitLat() & 0xffff);
}
@Override
public boolean equals(final Object obj)
{
if (obj == null) return false;
if (obj == this) return true;
if (obj.getClass() != getClass()) return false;
final SsUsbDeviceCapabilityDescriptor other =
(SsUsbDeviceCapabilityDescriptor) obj;
return new EqualsBuilder()
.append(bDescriptorType(), other.bDescriptorType())
.append(bLength(), other.bLength())
.append(bDevCapabilityType(), other.bDevCapabilityType())
.append(bmAttributes(), other.bmAttributes())
.append(wSpeedSupported(), other.wSpeedSupported())
.append(bFunctionalitySupport(), other.bFunctionalitySupport())
.append(bU1DevExitLat(), other.bU1DevExitLat())
.append(bU2DevExitLat(), other.bU2DevExitLat())
.isEquals();
return String.format(
"SuperSpeed USB Device Capability Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" bDevCapabilityType %7d%n" +
" bmAttributes %13s%n" +
" wSpeedSupported %10d%n" +
" bFunctionalitySupport %4d%n" +
" bU1DevExitLat %12d%n" +
" bU2DevExitLat %12d%n",
this.bLength() & 0xFF,
this.bDescriptorType() & 0xFF,
this.bDevCapabilityType() & 0xFF,
String.format("0x%02x", this.bmAttributes() & 0xFF),
this.wSpeedSupported() & 0xFFFF,
this.bFunctionalitySupport() & 0xFF,
this.bU1DevExitLat() & 0xFF,
this.bU2DevExitLat() & 0xFFFF);
}
@Override
public int hashCode()
{
return new HashCodeBuilder()
.append(bLength())
.append(bDescriptorType())
.append(bDevCapabilityType())
.append(bmAttributes())
.append(wSpeedSupported())
.append(bFunctionalitySupport())
.append(bU1DevExitLat())
.append(bU2DevExitLat())
.append(this.bLength())
.append(this.bDescriptorType())
.append(this.bDevCapabilityType())
.append(this.bmAttributes())
.append(this.wSpeedSupported())
.append(this.bFunctionalitySupport())
.append(this.bU1DevExitLat())
.append(this.bU2DevExitLat())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (obj == null)
{
return false;
}
if (obj == this)
{
return true;
}
if (obj.getClass() != this.getClass())
{
return false;
}
final SsUsbDeviceCapabilityDescriptor other = (SsUsbDeviceCapabilityDescriptor) obj;
return new EqualsBuilder()
.append(this.bLength(), other.bLength())
.append(this.bDescriptorType(), other.bDescriptorType())
.append(this.bDevCapabilityType(), other.bDevCapabilityType())
.append(this.bmAttributes(), other.bmAttributes())
.append(this.wSpeedSupported(), other.wSpeedSupported())
.append(this.bFunctionalitySupport(), other.bFunctionalitySupport())
.append(this.bU1DevExitLat(), other.bU1DevExitLat())
.append(this.bU2DevExitLat(), other.bU2DevExitLat())
.isEquals();
}
@Override
public String toString()
{
return dump();
return this.dump();
}
}

View File

@ -1,9 +1,10 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* Copyright 2013 Luca Longinotti <l@longi.li>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -22,7 +23,7 @@ import java.nio.ByteBuffer;
/**
* The generic USB transfer structure.
*
*
* The user populates this structure and then submits it in order to request a
* transfer. After the transfer has completed, the library populates the
* transfer with the results and passes it back to the user.
@ -32,36 +33,42 @@ 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
}
/**
* Returns the native pointer.
*
*
* @return The native pointer.
*/
public long getPointer()
{
return this.pointer;
return this.transferPointer;
}
/**
* Returns the handle of the device that this transfer will be submitted to.
*
*
* @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.
*
*
* @param handle
* The handle of the device.
*/
@ -69,131 +76,258 @@ public final class Transfer
/**
* Returns the bitwise OR combination of libusb transfer flags.
*
*
* @return The transfer flags.
*/
public native int getFlags();
public native byte flags();
/**
* Sets the bitwise OR combination of libusb transfer flags.
*
*
* @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.
*
*
* @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.
*
*
* @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
* indicates no timeout.
*
*
* @return The timeout.
*/
public native long getTimeout();
public native long timeout();
/**
* Sets the timeout for this transfer in milliseconds. A value of 0
* indicates no timeout.
*
*
* @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
* transfer callback function.
*
*
* If this is an isochronous transfer, this field may read
* {@link LibUsb#TRANSFER_COMPLETED} even if there were errors in the
* frames. Use the status field in each packet to determine if errors
* occurred.
*
*
* @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 (this.transferBuffer == null)
{
throw new IllegalArgumentException(
"buffer is null, only a length of 0 is allowed");
}
if (this.transferBuffer.capacity() < length)
{
throw new IllegalArgumentException(
"buffer too small for requested length");
}
}
// Native call.
this.setLengthNative(length);
}
native void setLengthNative(final int length);
/**
* Returns the actual length of data that was transferred. Read-only, and
* only for use within transfer callback function. Not valid for isochronous
* endpoint transfers.
*
*
* @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 this.transferBuffer;
}
/**
* Sets the data buffer.
*
*
* @param buffer
* The data buffer to set.
*/
public native void setBuffer(final ByteBuffer buffer);
public void setBuffer(final ByteBuffer buffer)
{
// Native call.
this.setBufferNative(buffer);
if (buffer != null)
{
// Set new length based on buffer's capacity.
this.setLengthNative(buffer.capacity());
}
else
{
this.setLengthNative(0);
}
// Once we know the native calls have gone through, update the
// reference.
this.transferBuffer = buffer;
}
native void setBufferNative(final ByteBuffer buffer);
/**
* Returns the number of isochronous packets. Only used for I/O with
* isochronous endpoints.
*
*
* @return The number of isochronous packets.
*/
public native int getNumIsoPackets();
public native int numIsoPackets();
/**
* Sets the number of isochronous packets.
*
*
* @param numIsoPackets
* 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) (this.transferPointer ^ (this.transferPointer >>> 32));
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (this.getClass() != obj.getClass())
{
return false;
}
final Transfer other = (Transfer) obj;
if (this.transferPointer != other.transferPointer)
{
return false;
}
return true;
}
@Override
public String toString()
{
return String.format("libusb transfer 0x%x", this.transferPointer);
}
}

View File

@ -0,0 +1,11 @@
/*
* Copyright 2013 Luca Longinotti <l@longi.li>
* See LICENSE.md for licensing information.
*/
package de.ailis.usb4java.libusb;
public interface TransferCallback
{
void processTransfer(Transfer transfer);
}

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -24,9 +24,9 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* A structure representing the USB 2.0 Extension descriptor. This descriptor is
* documented in section 9.6.2.1 of the USB 3.0 specification.
*
*
* All multiple-byte fields are represented in host-endian format.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class Usb20ExtensionDescriptor
@ -36,8 +36,8 @@ public final class Usb20ExtensionDescriptor
/**
* Constructs a new USB 2.0 Extension descriptor which can be passed to the
* {@link LibUsb#getUsb20ExtensionDescriptor(Context,
* BosDevCapabilityDescriptor, Usb20ExtensionDescriptor)} method.
* {@link LibUsb#getUsb20ExtensionDescriptor(Context, BosDevCapabilityDescriptor, Usb20ExtensionDescriptor)}
* method.
*/
public Usb20ExtensionDescriptor()
{
@ -46,7 +46,7 @@ public final class Usb20ExtensionDescriptor
/**
* Returns the native pointer.
*
*
* @return The native pointer.
*/
public long getPointer()
@ -56,79 +56,91 @@ public final class Usb20ExtensionDescriptor
/**
* Returns the size of this descriptor (in bytes).
*
*
* @return The descriptor size in bytes;
*/
public native byte bLength();
/**
* Returns the descriptor type.
*
*
* @return The descriptor type.
*/
public native byte bDescriptorType();
/**
* Returns the device capability type.
*
*
* @return The device capability type.
*/
public native byte bDevCapabilityType();
/**
* Returns the bitmap of supported device level features.
*
* Returns the bitmap of supported device level features.
*
* @return The supported device level features.
*/
public native int bmAttributes();
/**
* Returns a dump of this descriptor.
*
*
* @return The descriptor dump.
*/
public String dump()
{
return String.format("USB 2.0 Extension Descriptor:%n"
+ " bLength %18d%n"
+ " bDescriptorType %10d%n"
+ " bDevCapabilityType %7d%n"
+ " bmAttributes %13s%n",
bLength() & 0xff,
bDescriptorType() & 0xff,
bDevCapabilityType() & 0xff,
String.format("0x%08x", bmAttributes()));
}
@Override
public boolean equals(final Object obj)
{
if (obj == null) return false;
if (obj == this) return true;
if (obj.getClass() != getClass()) return false;
final Usb20ExtensionDescriptor other =
(Usb20ExtensionDescriptor) obj;
return new EqualsBuilder()
.append(bDescriptorType(), other.bDescriptorType())
.append(bLength(), other.bLength())
.append(bDevCapabilityType(), other.bDevCapabilityType())
.append(bmAttributes(), other.bmAttributes()).isEquals();
return String.format(
"USB 2.0 Extension Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" bDevCapabilityType %7d%n" +
" bmAttributes %13s%n",
this.bLength() & 0xFF,
this.bDescriptorType() & 0xFF,
this.bDevCapabilityType() & 0xFF,
String.format("0x%08x", this.bmAttributes()));
}
@Override
public int hashCode()
{
return new HashCodeBuilder()
.append(bLength())
.append(bDescriptorType())
.append(bDevCapabilityType())
.append(bmAttributes())
.append(this.bLength())
.append(this.bDescriptorType())
.append(this.bDevCapabilityType())
.append(this.bmAttributes())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (obj == null)
{
return false;
}
if (obj == this)
{
return true;
}
if (obj.getClass() != this.getClass())
{
return false;
}
final Usb20ExtensionDescriptor other = (Usb20ExtensionDescriptor) obj;
return new EqualsBuilder()
.append(this.bLength(), other.bLength())
.append(this.bDescriptorType(), other.bDescriptorType())
.append(this.bDevCapabilityType(), other.bDevCapabilityType())
.append(this.bmAttributes(), other.bmAttributes())
.isEquals();
}
@Override
public String toString()
{
return dump();
return this.dump();
}
}

View File

@ -1,9 +1,9 @@
/*
* Copyright 2013 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://www.libusb.org/>:
*
*
* Based on libusb <http://www.libusb.org/>:
*
* Copyright 2001 Johannes Erdfelt <johannes@erdfelt.com>
* Copyright 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright 2010-2012 Peter Stuge <peter@stuge.se>
@ -24,14 +24,14 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* Structure providing the version of the libusb runtime.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
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()}.
@ -43,77 +43,113 @@ public final class Version implements Comparable<Version>
/**
* Returns the native pointer.
*
*
* @return The native pointer.
*/
public long getPointer()
{
return this.pointer;
return this.versionPointer;
}
/**
* Returns the library major version.
*
*
* @return The library major version.
*/
public native int major();
/**
* Returns the library minor version.
*
*
* @return The library minor version.
*/
public native int minor();
/**
* Returns the library micro version.
*
*
* @return The library micro 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".
*
*
* @return The release candidate suffix string.
*/
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(this.major())
.append(this.minor())
.append(this.micro())
.append(this.nano())
.append(this.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 (this.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(rc(), other.rc())
.append(this.major(), other.major())
.append(this.minor(), other.minor())
.append(this.micro(), other.micro())
.append(this.nano(), other.nano())
.append(this.rc(), other.rc())
.isEquals();
}
@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(rc(), other.rc())
.append(this.major(), other.major())
.append(this.minor(), other.minor())
.append(this.micro(), other.micro())
.append(this.nano(), other.nano())
.append(this.rc(), other.rc())
.toComparison();
}
@Override
public String toString()
{
return this.major() + "." + this.minor() + "." + this.micro() + "."
+ this.nano() + this.rc();
}
}

View File

@ -7,3 +7,4 @@
* Low-Level classes based on the native libusb library.
*/
package de.ailis.usb4java.libusb;

View File

@ -0,0 +1,48 @@
/*
* Copyright 2013 Luca Longinotti <l@longi.li>
* See LICENSE.md for licensing information.
*/
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

@ -18,21 +18,21 @@ import de.ailis.usb4java.libusb.LibUsb;
/**
* Utility methods used for descriptor dumps.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
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 +41,11 @@ 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_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");
}
/**
@ -61,59 +59,70 @@ public final class DescriptorUtils
/**
* Returns the name of the specified USB class. "unknown" is returned for a
* class which is unknown to libusb.
*
*
* @param usbClass
* 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;
}
/**
* Decodes a binary-coded decimal into a string and returns it.
*
*
* @param bcd
* 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);
}
/**
* Dumps the specified byte buffer into a hex string and returns it.
*
*
* @param bytes
* The bytes to dump.
* @return The hex dump.
*/
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.
* @return The descriptor dump.
@ -124,9 +133,8 @@ 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.
* @param manufacturer
@ -140,62 +148,64 @@ public final class DescriptorUtils
public static String dump(final UsbDeviceDescriptor descriptor,
final String manufacturer, final String product, final String serial)
{
return String.format("Device Descriptor:%n"
+ " bLength %18d%n"
+ " bDescriptorType %10d%n"
+ " bcdUSB %19s%n"
+ " bDeviceClass %13d %s%n"
+ " bDeviceSubClass %10d%n"
+ " bDeviceProtocol %10d%n"
+ " bMaxPacketSize0 %10d%n"
+ " idVendor %17s%n"
+ " idProduct %16s%n"
+ " bcdDevice %16s%n"
+ " iManufacturer %12d%s%n"
+ " iProduct %17d%s%n"
+ " iSerial %18d%s%n"
+ " bNumConfigurations %7d%n",
return String.format(
"Device Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" bcdUSB %19s%n" +
" bDeviceClass %13d %s%n" +
" bDeviceSubClass %10d%n" +
" bDeviceProtocol %10d%n" +
" bMaxPacketSize0 %10d%n" +
" idVendor %17s%n" +
" idProduct %16s%n" +
" bcdDevice %16s%n" +
" iManufacturer %12d%s%n" +
" iProduct %17d%s%n" +
" iSerial %18d%s%n" +
" bNumConfigurations %7d%n",
descriptor.bLength(),
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);
}
/**
* Dumps the specified USB configuration descriptor into a string and
* returns it.
*
*
* @param descriptor
* The USB configuration descriptor to dump.
* @return The descriptor dump.
*/
public static String dump(final UsbConfigurationDescriptor descriptor)
{
return String.format("Configuration Descriptor:%n"
+ " bLength %18d%n"
+ " bDescriptorType %10d%n"
+ " wTotalLength %13d%n"
+ " bNumInterfaces %11d%n"
+ " bConfigurationValue %6d%n"
+ " iConfiguration %11d%n"
+ " bmAttributes %13s%n"
+ " %s%n"
+ "%s"
+ " bMaxPower %16smA%n",
return String.format(
"Configuration Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" wTotalLength %13d%n" +
" bNumInterfaces %11d%n" +
" bConfigurationValue %6d%n" +
" iConfiguration %11d%n" +
" bmAttributes %13s%n" +
" %s%n" +
"%s" +
" bMaxPower %16smA%n",
descriptor.bLength(),
descriptor.bDescriptorType(),
descriptor.wTotalLength() & 0xffff,
@ -203,74 +213,76 @@ 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.
* @return The descriptor dump.
*/
public static String dump(final UsbInterfaceDescriptor descriptor)
{
return String.format("Interface Descriptor:%n"
+ " bLength %18d%n"
+ " bDescriptorType %10d%n"
+ " bInterfaceNumber %9d%n"
+ " bAlternateSetting %8d%n"
+ " bNumEndpoints %12d%n"
+ " bInterfaceClass %10d %s%n"
+ " bInterfaceSubClass %7d%n"
+ " bInterfaceProtocol %7d%n"
+ " iInterface %15d%n",
return String.format(
"Interface Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" bInterfaceNumber %9d%n" +
" bAlternateSetting %8d%n" +
" bNumEndpoints %12d%n" +
" bInterfaceClass %10d %s%n" +
" bInterfaceSubClass %7d%n" +
" bInterfaceProtocol %7d%n" +
" iInterface %15d%n",
descriptor.bLength(),
descriptor.bDescriptorType(),
descriptor.bInterfaceNumber() & 0xff,
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.
* @return The descriptor dump.
*/
public static String dump(final UsbEndpointDescriptor descriptor)
{
return String.format("Endpoint Descriptor:%n"
+ " bLength %18d%n"
+ " bDescriptorType %10d%n"
+ " bEndpointAddress %9s EP %d %s%n"
+ " bmAttributes %13d%n"
+ " Transfer Type %s%n"
+ " Synch Type %s%n"
+ " Usage Type %s%n"
+ " wMaxPacketSize %11d%n"
+ " bInterval %16d%n",
return String.format(
"Endpoint Descriptor:%n" +
" bLength %18d%n" +
" bDescriptorType %10d%n" +
" bEndpointAddress %9s EP %d %s%n" +
" bmAttributes %13d%n" +
" Transfer Type %s%n" +
" Synch Type %s%n" +
" Usage Type %s%n" +
" wMaxPacketSize %11d%n" +
" bInterval %16d%n",
descriptor.bLength(),
descriptor.bDescriptorType(),
String.format("0x%02x", descriptor.bEndpointAddress() & 0xff),
descriptor.bEndpointAddress() & 0xf,
(descriptor.bEndpointAddress() & 0x80) == 0 ? "OUT" : "IN",
descriptor.bEndpointAddress() & 0x0f,
((descriptor.bEndpointAddress() & LibUsb.ENDPOINT_IN) == 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);
}
@ -278,74 +290,80 @@ public final class DescriptorUtils
/**
* Returns the name for the transfer type in the specified endpoint
* attributes.
*
*
* @param bmAttributes
* 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";
}
}
/**
* Returns the name for the synchronization type in the specified endpoint
* attributes.
*
*
* @param bmAttributes
* 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";
}
}
/**
* Returns the name for the usage type in the specified endpoint attributes.
*
*
* @param bmAttributes
* 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";
}
}
/**
* Returns the name for the specified speed number.
*
*
* @param speed
* The speed number.
* @return The speed name.

View File

@ -13,7 +13,7 @@ import org.junit.Test;
/**
* Tests the {@link BosDescriptor} class.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public class BosDescriptorTest
@ -31,19 +31,18 @@ public class BosDescriptorTest
LibUsb.init(null);
this.descriptor = new BosDescriptor();
}
/**
* Tear down test.
*/
@After
@After
public void tearDown()
{
LibUsb.exit(null);
}
/**
* Tests uninitialized access to
* {@link BosDescriptor#bLength()}
* Tests uninitialized access to {@link BosDescriptor#bLength()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedLength()
@ -53,8 +52,7 @@ public class BosDescriptorTest
}
/**
* Tests uninitialized access to
* {@link BosDescriptor#bDescriptorType()}
* Tests uninitialized access to {@link BosDescriptor#bDescriptorType()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorType()
@ -64,8 +62,7 @@ public class BosDescriptorTest
}
/**
* Tests uninitialized access to
* {@link BosDescriptor#wTotalLength()}
* Tests uninitialized access to {@link BosDescriptor#wTotalLength()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedTotalLength()
@ -75,8 +72,7 @@ public class BosDescriptorTest
}
/**
* Tests uninitialized access to
* {@link BosDescriptor#bNumDeviceCaps()}
* Tests uninitialized access to {@link BosDescriptor#bNumDeviceCaps()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedNumDeviceCaps()
@ -86,8 +82,7 @@ public class BosDescriptorTest
}
/**
* Tests uninitialized access to
* {@link BosDescriptor#devCapability()}
* Tests uninitialized access to {@link BosDescriptor#devCapability()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDevCapability()

View File

@ -13,7 +13,7 @@ import org.junit.Test;
/**
* Tests the {@link BosDevCapabilityDescriptor} class.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public class BosDevCapabilityDescriptorTest
@ -31,11 +31,11 @@ public class BosDevCapabilityDescriptorTest
LibUsb.init(null);
this.descriptor = new BosDevCapabilityDescriptor();
}
/**
* Tear down test.
*/
@After
@After
public void tearDown()
{
LibUsb.exit(null);

View File

@ -13,7 +13,7 @@ import org.junit.Test;
/**
* Tests the {@link ConfigDescriptor} class.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public class ConfigDescriptorTest
@ -31,19 +31,18 @@ public class ConfigDescriptorTest
LibUsb.init(null);
this.descriptor = new ConfigDescriptor();
}
/**
* Tear down test.
*/
@After
@After
public void tearDown()
{
LibUsb.exit(null);
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#bLength()}
* Tests uninitialized access to {@link ConfigDescriptor#bLength()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedLength()
@ -53,8 +52,7 @@ public class ConfigDescriptorTest
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#bDescriptorType()}
* Tests uninitialized access to {@link ConfigDescriptor#bDescriptorType()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorType()
@ -64,8 +62,7 @@ public class ConfigDescriptorTest
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#wTotalLength()}
* Tests uninitialized access to {@link ConfigDescriptor#wTotalLength()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedTotalLength()
@ -75,8 +72,7 @@ public class ConfigDescriptorTest
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#bNumInterfaces()}
* Tests uninitialized access to {@link ConfigDescriptor#bNumInterfaces()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedNumInterfaces()
@ -97,8 +93,7 @@ public class ConfigDescriptorTest
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#iConfiguration()}
* Tests uninitialized access to {@link ConfigDescriptor#iConfiguration()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedConfiguration()
@ -108,8 +103,7 @@ public class ConfigDescriptorTest
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#bmAttributes()}
* Tests uninitialized access to {@link ConfigDescriptor#bmAttributes()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorAttributes()
@ -119,8 +113,7 @@ public class ConfigDescriptorTest
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#bMaxPower()}
* Tests uninitialized access to {@link ConfigDescriptor#bMaxPower()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorMaxPower()
@ -130,8 +123,7 @@ public class ConfigDescriptorTest
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#iface()}
* Tests uninitialized access to {@link ConfigDescriptor#iface()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorIface()
@ -141,8 +133,7 @@ public class ConfigDescriptorTest
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#extra()}
* Tests uninitialized access to {@link ConfigDescriptor#extra()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorExtra()
@ -152,8 +143,7 @@ public class ConfigDescriptorTest
}
/**
* Tests uninitialized access to
* {@link ConfigDescriptor#extraLength()}
* Tests uninitialized access to {@link ConfigDescriptor#extraLength()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorExtraLength()

View File

@ -13,7 +13,7 @@ import org.junit.Test;
/**
* Tests the {@link ContainerIdDescriptor} class.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public class ContainerIdDescriptorTest
@ -31,19 +31,18 @@ public class ContainerIdDescriptorTest
LibUsb.init(null);
this.descriptor = new ContainerIdDescriptor();
}
/**
* Tear down test.
*/
@After
@After
public void tearDown()
{
LibUsb.exit(null);
}
/**
* Tests uninitialized access to
* {@link ContainerIdDescriptor#bLength()}
* Tests uninitialized access to {@link ContainerIdDescriptor#bLength()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedLength()
@ -75,8 +74,7 @@ public class ContainerIdDescriptorTest
}
/**
* Tests uninitialized access to
* {@link ContainerIdDescriptor#bReserved()}
* Tests uninitialized access to {@link ContainerIdDescriptor#bReserved()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedReserved()
@ -86,8 +84,7 @@ public class ContainerIdDescriptorTest
}
/**
* Tests uninitialized access to
* {@link ContainerIdDescriptor#containerId()}
* Tests uninitialized access to {@link ContainerIdDescriptor#containerId()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedContainerId()

View File

@ -13,7 +13,7 @@ import org.junit.Test;
/**
* Tests the {@link DeviceDescriptor} class.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public class DeviceDescriptorTest
@ -31,19 +31,18 @@ public class DeviceDescriptorTest
LibUsb.init(null);
this.descriptor = new DeviceDescriptor();
}
/**
* Tear down test.
*/
@After
@After
public void tearDown()
{
LibUsb.exit(null);
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bLength()}
* Tests uninitialized access to {@link DeviceDescriptor#bLength()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedLength()
@ -53,8 +52,7 @@ public class DeviceDescriptorTest
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bDescriptorType()}
* Tests uninitialized access to {@link DeviceDescriptor#bDescriptorType()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDescriptorType()
@ -64,8 +62,7 @@ public class DeviceDescriptorTest
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bcdUSB()}
* Tests uninitialized access to {@link DeviceDescriptor#bcdUSB()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedBcdUSB()
@ -75,8 +72,7 @@ public class DeviceDescriptorTest
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bDeviceClass()}
* Tests uninitialized access to {@link DeviceDescriptor#bDeviceClass()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDeviceClass()
@ -86,8 +82,7 @@ public class DeviceDescriptorTest
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bDeviceSubClass()}
* Tests uninitialized access to {@link DeviceDescriptor#bDeviceSubClass()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDeviceSubClass()
@ -97,8 +92,7 @@ public class DeviceDescriptorTest
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bDeviceProtocol()}
* Tests uninitialized access to {@link DeviceDescriptor#bDeviceProtocol()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedDeviceProtocol()
@ -108,8 +102,7 @@ public class DeviceDescriptorTest
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bMaxPacketSize0()}
* Tests uninitialized access to {@link DeviceDescriptor#bMaxPacketSize0()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedMaxPacketSize0()
@ -119,8 +112,7 @@ public class DeviceDescriptorTest
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#idVendor()}
* Tests uninitialized access to {@link DeviceDescriptor#idVendor()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedVendor()
@ -130,8 +122,7 @@ public class DeviceDescriptorTest
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#idProduct()}
* Tests uninitialized access to {@link DeviceDescriptor#idProduct()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedProduct()
@ -141,8 +132,7 @@ public class DeviceDescriptorTest
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#bcdDevice()}
* Tests uninitialized access to {@link DeviceDescriptor#bcdDevice()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedBcdDevice()
@ -152,8 +142,7 @@ public class DeviceDescriptorTest
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#iManufacturer()}
* Tests uninitialized access to {@link DeviceDescriptor#iManufacturer()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedManufacturer()
@ -163,8 +152,7 @@ public class DeviceDescriptorTest
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#iProduct()}
* Tests uninitialized access to {@link DeviceDescriptor#iProduct()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedIProduct()
@ -174,8 +162,7 @@ public class DeviceDescriptorTest
}
/**
* Tests uninitialized access to
* {@link DeviceDescriptor#iSerialNumber()}
* Tests uninitialized access to {@link DeviceDescriptor#iSerialNumber()}
*/
@Test(expected = IllegalStateException.class)
public void testUninitializedSerialNumber()

Some files were not shown because too many files have changed in this diff Show More