Permalink
Browse files

Restructuring a bit.

  • Loading branch information...
trygvis committed Mar 28, 2010
1 parent af088c4 commit c86ae540820694c34171d823c0e6575d6801c464
View
@@ -1,4 +1,17 @@
-TCK Notes
+= Implementation Notes =
+
+Right now there's a whole lot of libusb_open() and libusb_close() going on. I
+wonder if that has a big impact on anything. If it has I have to find a way to
+associate a handle with a device or something. However, as there are no
+close() operations on a device it's kinda hard to know when to release the
+handle.
+
+= TCK Notes =
* The TCK should not assert if a method is synchronized or not.
* The TCK should not assert private methods. They are PRIVATE.
+
+Stuff I know I don't do properly that the TCK should catch:
+
+ * When closing a parent object (like a UsbDevice) it should implicitly close
+ all child objects (like any UsbPipes that are open).
@@ -35,10 +35,13 @@ public void close() throws IOException {
UsbUtil.close(device);
}
- public void setBaudRate(int requestedBaudRate) {
- int value = calculateBaudRate(requestedBaudRate);
+ public void setBaudRate(int requestedBaudRate) throws UsbException {
+ short value = (short) (calculateBaudRate(requestedBaudRate) >> 16);
+ short index = 0;
+
+ byte outRequestType = ENDPOINT_DIRECTION_OUT | REQUESTTYPE_TYPE_VENDOR | REQUESTTYPE_RECIPIENT_DEVICE;
- throw new RuntimeException("Not implemented");
+ device.syncSubmit(device.createUsbControlIrp(outRequestType, SET_BAUDRATE_REQUEST, value, index));
}
public void setDtr(boolean b) throws UsbException {
@@ -47,14 +50,16 @@ public void setDtr(boolean b) throws UsbException {
byte outRequestType = ENDPOINT_DIRECTION_OUT | REQUESTTYPE_TYPE_VENDOR | REQUESTTYPE_RECIPIENT_DEVICE;
- UsbPipe pipe = controlEndpoint.getUsbPipe();
- try {
- pipe.open();
- UsbControlIrp irp = pipe.createUsbControlIrp(outRequestType, SET_MODEM_CONTROL_REQUEST, value, index);
- irp.setData(new byte[0], 0, 0);
- pipe.syncSubmit(irp);
- } finally {
- UsbUtil.close(pipe);
- }
+ device.syncSubmit(device.createUsbControlIrp(outRequestType, SET_MODEM_CONTROL_REQUEST, value, index));
+
+// UsbPipe pipe = controlEndpoint.getUsbPipe();
+// try {
+// pipe.open();
+// UsbControlIrp irp = pipe.createUsbControlIrp(outRequestType, SET_MODEM_CONTROL_REQUEST, value, index);
+// irp.setData(new byte[0], 0, 0);
+// pipe.syncSubmit(irp);
+// } finally {
+// UsbUtil.close(pipe);
+// }
}
}
@@ -6,7 +6,7 @@
public static int calculateBaudRate(int requestedBaudRate) {
int base = 3000000 / requestedBaudRate;
- return base;
+ return base << 16;
}
public static UsbDevice findDevice(UsbHub usbHub) {
@@ -0,0 +1,25 @@
+package no.hackaton.ftdi;
+
+import static java.lang.Thread.*;
+
+import javax.usb.*;
+
+public class ReadSerial {
+ public static void main(String[] args) throws Exception {
+ UsbServices usbServices = UsbHostManager.getUsbServices();
+
+ UsbDevice device = Ftdi232Util.findDevice(usbServices.getRootUsbHub());
+
+ if(device == null) {
+ System.err.println("Could not find appropriate FTDI device.");
+ return;
+ }
+
+ Ftdi232 ftdi232 = new Ftdi232(device);
+
+ ftdi232.setBaudRate(2400);
+ ftdi232.setDtr(true);
+ sleep(1000);
+ ftdi232.setDtr(false);
+ }
+}
@@ -17,8 +17,12 @@ public static void main(String[] args) throws Exception {
Ftdi232 ftdi232 = new Ftdi232(device);
+ System.out.println("Setting DRT");
+ System.out.flush();
ftdi232.setDtr(true);
sleep(1000);
+ System.out.println("Clearing DRT");
+ System.out.flush();
ftdi232.setDtr(false);
}
}
View
@@ -20,7 +20,7 @@ javalibusb.lo: javalibusb.c
usbw.lo: usbw.c
$(LIBTOOL) --mode=compile --tag=CC cc $(CCFLAGS) -g -c -shared -o $@ $<
-javalibusb.c: javalibusb1_libusb1.h javalibusb1_impl_Libusb1UsbDevice.h javalibusb1_impl_Libusb1UsbInterface.h javalibusb1_impl_Libusb1UsbPipe.h
+javalibusb.c: javalibusb1_libusb1.h javalibusb1_Libusb1UsbDevice.h javalibusb1_Libusb1UsbInterface.h javalibusb1_Libusb1UsbPipe.h
target/classes/javalibusb1/%.class: src/main/java/javalibusb1/libusb1.java
mkdir -p target/classes target/test-classes
@@ -29,13 +29,13 @@ target/classes/javalibusb1/%.class: src/main/java/javalibusb1/libusb1.java
rsync -a --progress src/main/resources/ target/classes
%.h:
- rm $@
+ rm -f $@
$(JDK_HOME)/bin/javah -classpath $(CLASSPATH) -jni $(shell echo $< | sed "s,target.classes.\(.*\).class$$,\1," | tr "/" ".")
javalibusb1_libusb1.h: target/classes/javalibusb1/libusb1.class
-javalibusb1_impl_Libusb1UsbDevice.h: target/classes/javalibusb1/impl/Libusb1UsbDevice.class
-javalibusb1_impl_Libusb1UsbInterface.h: target/classes/javalibusb1/impl/Libusb1UsbInterface.class
-javalibusb1_impl_Libusb1UsbPipe.h: target/classes/javalibusb1/impl/Libusb1UsbPipe.class
+javalibusb1_Libusb1UsbDevice.h: target/classes/javalibusb1/Libusb1UsbDevice.class
+javalibusb1_Libusb1UsbInterface.h: target/classes/javalibusb1/Libusb1UsbInterface.class
+javalibusb1_Libusb1UsbPipe.h: target/classes/javalibusb1/Libusb1UsbPipe.class
clean: clean-java clean-native
View
@@ -9,21 +9,21 @@
/* javalibusb1.Libusb1UsbServices */
jfieldID usb_services_context_field;
-/* javalibusb1.impl.Libusb1UsbDevice */
+/* javalibusb1.Libusb1UsbDevice */
jclass libusb1UsbDeviceClass = NULL;
jmethodID libusb1UsbDeviceConstructor = NULL;
jfieldID device_libusb_device_field;
-/* javalibusb1.impl.Libusb1UsbConfiguration */
+/* javalibusb1.Libusb1UsbConfiguration */
jclass libusb1UsbConfigurationClass = NULL;
jmethodID libusb1UsbConfigurationConstructor = NULL;
-/* javalibusb1.impl.Libusb1UsbInterface */
+/* javalibusb1.Libusb1UsbInterface */
jclass libusb1UsbInterfaceClass = NULL;
jmethodID libusb1UsbInterfaceConstructor = NULL;
jfieldID interface_libusb_device_field;
-/* javalibusb1.impl.Libusb1UsbEndpoint */
+/* javalibusb1.Libusb1UsbEndpoint */
jclass libusb1UsbEndpointClass = NULL;
jmethodID libusb1UsbEndpointConstructor = NULL;
@@ -267,7 +267,7 @@ JNIEXPORT jobject JNICALL Java_javalibusb1_libusb1_create
goto fail;
}
- if((libusb1UsbDeviceClass = findAndReferenceClass(env, "javalibusb1/impl/Libusb1UsbDevice")) == NULL) {
+ if((libusb1UsbDeviceClass = findAndReferenceClass(env, "javalibusb1/Libusb1UsbDevice")) == NULL) {
return NULL;
}
if((libusb1UsbDeviceConstructor = (*env)->GetMethodID(env, libusb1UsbDeviceClass, "<init>", "(IBBLjavax/usb/UsbDeviceDescriptor;)V")) == NULL) {
@@ -277,27 +277,27 @@ JNIEXPORT jobject JNICALL Java_javalibusb1_libusb1_create
goto fail;
}
- if((libusb1UsbConfigurationClass = findAndReferenceClass(env, "javalibusb1/impl/Libusb1UsbConfiguration")) == NULL) {
+ if((libusb1UsbConfigurationClass = findAndReferenceClass(env, "javalibusb1/Libusb1UsbConfiguration")) == NULL) {
return NULL;
}
- if((libusb1UsbConfigurationConstructor = (*env)->GetMethodID(env, libusb1UsbConfigurationClass, "<init>", "(Ljavalibusb1/impl/Libusb1UsbDevice;Ljavax/usb/UsbConfigurationDescriptor;[[Ljavax/usb/UsbInterface;Z)V")) == NULL) {
+ if((libusb1UsbConfigurationConstructor = (*env)->GetMethodID(env, libusb1UsbConfigurationClass, "<init>", "(Ljavalibusb1/Libusb1UsbDevice;Ljavax/usb/UsbConfigurationDescriptor;[[Ljavax/usb/UsbInterface;Z)V")) == NULL) {
return NULL;
}
- if((libusb1UsbInterfaceClass = findAndReferenceClass(env, "javalibusb1/impl/Libusb1UsbInterface")) == NULL) {
+ if((libusb1UsbInterfaceClass = findAndReferenceClass(env, "javalibusb1/Libusb1UsbInterface")) == NULL) {
return NULL;
}
- if((libusb1UsbInterfaceConstructor = (*env)->GetMethodID(env, libusb1UsbInterfaceClass, "<init>", "(Ljavalibusb1/impl/Libusb1UsbConfiguration;Ljavax/usb/UsbInterfaceDescriptor;[Ljavax/usb/UsbEndpoint;Z)V")) == NULL) {
+ if((libusb1UsbInterfaceConstructor = (*env)->GetMethodID(env, libusb1UsbInterfaceClass, "<init>", "(Ljavalibusb1/Libusb1UsbConfiguration;Ljavax/usb/UsbInterfaceDescriptor;[Ljavax/usb/UsbEndpoint;Z)V")) == NULL) {
return NULL;
}
if((interface_libusb_device_field = (*env)->GetFieldID(env, libusb1UsbInterfaceClass, "libusb_device", "I")) == NULL) {
goto fail;
}
- if((libusb1UsbEndpointClass = findAndReferenceClass(env, "javalibusb1/impl/Libusb1UsbEndpoint")) == NULL) {
+ if((libusb1UsbEndpointClass = findAndReferenceClass(env, "javalibusb1/Libusb1UsbEndpoint")) == NULL) {
return NULL;
}
- if((libusb1UsbEndpointConstructor = (*env)->GetMethodID(env, libusb1UsbEndpointClass, "<init>", "(Ljavalibusb1/impl/Libusb1UsbInterface;Ljavax/usb/UsbEndpointDescriptor;)V")) == NULL) {
+ if((libusb1UsbEndpointConstructor = (*env)->GetMethodID(env, libusb1UsbEndpointClass, "<init>", "(Ljavalibusb1/Libusb1UsbInterface;Ljavax/usb/UsbEndpointDescriptor;)V")) == NULL) {
return NULL;
}
@@ -485,11 +485,34 @@ JNIEXPORT jobjectArray JNICALL Java_javalibusb1_libusb1_get_1devices
return NULL;
}
+JNIEXPORT jint JNICALL Java_javalibusb1_libusb1_control_1transfer
+ (JNIEnv *env, jobject obj, jint java_device, jbyte bmRequestType, jbyte bRequest, jshort wValue, jshort wIndex, jlong timeout)
+{
+ int err;
+ struct libusb_device* device;
+ struct libusb_device_handle *handle;
+
+ device = (struct libusb_device*)java_device;
+
+ if((err = usbw_open(device, &handle))) {
+ throwPlatformExceptionMsgCode(env, "libusb_open()", err);
+ return 0;
+ }
+
+ if((err = usbw_control_transfer((struct libusb_device_handle*)handle, bmRequestType, bRequest, wValue, wIndex, NULL, 0, timeout)) < 0) {
+ throwPlatformExceptionMsgCode(env, "libusb_control_transfer()", err);
+ }
+
+ usbw_close(handle);
+
+ return err;
+}
+
/*****************************************************************************
- * javalibusb1_impl_Libusb1UsbDevice
+ * javalibusb1_Libusb1UsbDevice
*****************************************************************************/
-JNIEXPORT void JNICALL Java_javalibusb1_impl_Libusb1UsbDevice_closeNative
+JNIEXPORT void JNICALL Java_javalibusb1_Libusb1UsbDevice_closeNative
(JNIEnv *env, jobject obj)
{
struct libusb_device *device;
@@ -505,7 +528,7 @@ JNIEXPORT void JNICALL Java_javalibusb1_impl_Libusb1UsbDevice_closeNative
usbw_unref_device(device);
}
-JNIEXPORT jstring JNICALL Java_javalibusb1_impl_Libusb1UsbDevice_getStringNative
+JNIEXPORT jstring JNICALL Java_javalibusb1_Libusb1UsbDevice_getStringNative
(JNIEnv *env, jobject obj, jbyte index, jint length)
{
struct libusb_device *device;
@@ -543,7 +566,7 @@ JNIEXPORT jstring JNICALL Java_javalibusb1_impl_Libusb1UsbDevice_getStringNative
return s;
}
-JNIEXPORT jobject JNICALL Java_javalibusb1_impl_Libusb1UsbDevice_nativeGetActiveUsbConfiguration
+JNIEXPORT jobject JNICALL Java_javalibusb1_Libusb1UsbDevice_nativeGetActiveUsbConfiguration
(JNIEnv *env, jobject usbDevice)
{
struct libusb_device *device;
@@ -575,7 +598,7 @@ JNIEXPORT jobject JNICALL Java_javalibusb1_impl_Libusb1UsbDevice_nativeGetActive
return usbConfiguration;
}
-JNIEXPORT jobject JNICALL Java_javalibusb1_impl_Libusb1UsbDevice_nativeGetUsbConfiguration
+JNIEXPORT jobject JNICALL Java_javalibusb1_Libusb1UsbDevice_nativeGetUsbConfiguration
(JNIEnv *env, jobject usbDevice, jbyte index)
{
struct libusb_device *device;
@@ -617,7 +640,7 @@ JNIEXPORT jobject JNICALL Java_javalibusb1_impl_Libusb1UsbDevice_nativeGetUsbCon
//
// -----------------------------------------------------------------------
-JNIEXPORT void JNICALL Java_javalibusb1_impl_Libusb1UsbInterface_nativeSetConfiguration
+JNIEXPORT void JNICALL Java_javalibusb1_Libusb1UsbInterface_nativeSetConfiguration
(JNIEnv *env, jobject obj, jint configuration)
{
struct libusb_device *device;
@@ -640,7 +663,7 @@ JNIEXPORT void JNICALL Java_javalibusb1_impl_Libusb1UsbInterface_nativeSetConfig
usbw_close(handle);
}
-JNIEXPORT jint JNICALL Java_javalibusb1_impl_Libusb1UsbInterface_nativeClaimInterface
+JNIEXPORT jint JNICALL Java_javalibusb1_Libusb1UsbInterface_nativeClaimInterface
(JNIEnv *env, jobject obj, jint bInterfaceNumber)
{
struct libusb_device *device;
@@ -666,32 +689,8 @@ JNIEXPORT jint JNICALL Java_javalibusb1_impl_Libusb1UsbInterface_nativeClaimInte
return 0;
}
-JNIEXPORT void JNICALL Java_javalibusb1_impl_Libusb1UsbInterface_nativeRelease
+JNIEXPORT void JNICALL Java_javalibusb1_Libusb1UsbInterface_nativeRelease
(JNIEnv *env, jobject obj, jint handle)
{
usbw_close((struct libusb_device_handle*)handle);
}
-
-/*****************************************************************************
- * javalibusb1_impl_Libusb1UsbPipe
- *****************************************************************************/
-
-/*
-JNIEXPORT jint JNICALL Java_javalibusb1_impl_Libusb1UsbPipe_nativeSyncSubmit
- (JNIEnv *env, jobject obj)
-{
-}
-*/
-
-JNIEXPORT jint JNICALL Java_javalibusb1_impl_Libusb1UsbPipe_nativeSyncSubmitControl
- (JNIEnv *env, jobject obj, jint handle, jbyte bmRequestType, jbyte bRequest, jshort wValue, jshort wIndex, jlong timeout)
-{
- int err;
-
- if((err = usbw_control_transfer((struct libusb_device_handle*)handle, bmRequestType, bRequest, wValue, wIndex, NULL, 0, timeout)) < 0) {
- throwPlatformExceptionMsgCode(env, "libusb_control_transfer()", err);
- return 0;
- }
-
- return err;
-}

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit c86ae54

Please sign in to comment.