Commit Graph

472 Commits

Author SHA1 Message Date
Luca Longinotti
032b7487d4 Add a few capability constants that were missing, from libusbx-1.0.15. 2013-06-12 14:44:24 +02:00
Luca Longinotti
81eea49213 Change to long, this makes sure there will be no collisions or problems in the HashMap even on 64bits.
On 32bits the pointer will only have at most 32bits set, so converting it to a void * (which also is 32bits) will not actually loose any information.
2013-06-12 14:07:42 +02:00
Luca Longinotti
275bc37a77 Right now if you add different PollfdListeners to different contexts, only the last one will be remembered, and subsequent callbacks, even from other contexts, will always use that last one.
This is obviously unexpected and incorrect, yet cannot be fixed by just adding more data on the C side: you'd have to remember the callback object at the very least, which you could in the user_data
field, but you'd have to make it a global reference. Yet, there would be no easy way to later correctly delete that global reference, since to unset the pollfdNotifiers you just pass NULL to the
same function again, and you don't get back any data on what was there before.
The easiest way to fix this was thus to change to a HashMap, Concurrent since access can be from random threads.
The key was chosen to be the Context.hashCode(), as Integer lookup and comparison is fast, and the hashCode depends directly on the contextPointer stored in the Context (which identifies it).
This works for sure on 32bit systems, but I'm still concerned about 64bit systems, where a long would probably make more sense, I need to think about it some more.
2013-06-12 13:38:10 +02:00
Luca Longinotti
a47e3cd3c3 Implement the remaining Control Transfer fill functions and the ControlSetup object.
Everything is done in Java here as it's much easier and cleaner, since those are only convenience functions/structs.
Byte order conversion is conveniently handled by ByteBuffer too, and using slice() we access the same memory all around.
Added some final keywords to LibUsb and BufferUtils.
2013-06-11 18:08:51 +02:00
Luca Longinotti
13e83fd59e Add isochronous transfer functions and async transfer fill functions. They are implemented directly in Java since they are static inline convenience functions in libusb.h and don't really need to call into C via JNI at all to work, this keeps the JNI layer simple and minimal.
Only fill_control/get_control is still outstanding.
getDescriptor() and getStringDescriptor() were removed from native JNI, since they are static inline convenience functions in libusb.h and don't do anything more than calling the appropriate real function.
2013-06-11 15:48:16 +02:00
Luca Longinotti
db62b8496b Add IsoPacketDescriptor.c to Makefile. 2013-06-11 13:14:04 +02:00
Luca Longinotti
95534657c9 Transfer constructor also package private, since you never create them but only get them through allocTransfer(). 2013-06-11 13:06:02 +02:00
Luca Longinotti
51c07e1dbc Add IsoPacketDescriptor objects and get them from the Transfer.
Also add equals/hashcode for Transfer and IsoPacketDescriptor.
2013-06-11 10:32:43 +02:00
Luca Longinotti
47543fa352 Constify wrapper functions (set and wrap). 2013-06-11 09:37:45 +02:00
Luca Longinotti
fddb83de2f Add submitTransfer and cancelTransfer. Rudimentary async I/O should now work (not tested yet!). 2013-06-10 23:32:51 +02:00
Luca Longinotti
d26a0e0d62 Fix NULL deref in InterfaceDescriptor too. 2013-06-10 20:25:17 +02:00
Luca Longinotti
09cf25cfae Fix NULL dereferences. Using the result of unwrap*() directly can lead to dereferencing NULL, for example if called on an already freed object.
InterfaceDescriptor still to do.
2013-06-10 17:42:27 +02:00
Luca Longinotti
cac3297458 Add BufferUtils to centralize creation of appropriate Buffers for LibUsb (various types, directly allocated). 2013-06-10 16:58:41 +02:00
Luca Longinotti
dca1ed7693 Finish work on Transfer implementation. Everything is consistent, fast callbacks are provided, support for FREE_TRANSFER is there and fully cleans up resources.
To support FREE_TRANSFER in the case the user doesn't set (or explicitely sets) a callback of null on the Java side, a small C-callback was created that just takes care of cleaning up the Java objects and resources.
2013-06-10 16:00:17 +02:00
Luca Longinotti
f04e3e4381 Rename trans to transfer inside LibUsb functions, like in Transfer.c.
Add methodID field to transfer_data structure, so that it can be cached at set() time and the callback is faster.
Also update the length when setting a new buffer!
2013-06-10 14:37:30 +02:00
Luca Longinotti
c1c32c3faf Add new transfer_data structure (Java and other objects). Update transfer functions accordingly.
Check all variable names for consistency.
Fix memory leak on failed getDeviceDescriptor.
Use result == LIBUSB_SUCCESS instead of just !result, it is much clearer what the integer that you get as result actually means that way.
2013-06-10 11:39:20 +02:00
Luca Longinotti
1b93df20ca Transfer.java: rename get methods to just be the name of the field with the appropriate type, like in the other classes (Descriptors etc.). This is consistent and easy to use.
Move callback and maxNumIsoPacketSize to C, minimizing what is kept in Java: only the pointer and the transferBuffer. setLength() is still checked here, as that's easiest.
Update the tests to match the changes and add documentation on the new methods.
2013-06-10 10:16:47 +02:00
Luca Longinotti
34262a1b00 Add work on async transfers, not yet completed, I may yet change where certain data is kept and how it is
checked.
2013-06-09 23:55:34 +02:00
Luca Longinotti
2ee0b2b7fd Also do a deep-equals of the arrays and ByteBuffers. 2013-06-09 23:35:20 +02:00
Luca Longinotti
dbd2a4cf55 Version.java: add nano().
*.java: Fix code and comment formatting to be equal everywhere and conform to what I could see as being the
expected usb4java standard.
*.java: Remove Apache-Commons dependency for extremely simple HashCode/Equals involving only the pointer
variable, instead use the Eclipse-generated methods.
2013-06-09 23:03:01 +02:00
Luca Longinotti
68125967a4 LibUsb.java: add missing MASKs, precise TRANSFER_FREE_BUFFER comment, only update references after native
calls return without throwing exceptions.
DescriptorUtils.java: update to use the correct masks and variables, always return "Unknown" on a not-handled
value.
*Descriptor.java: update equals/hashcode to use all fields in the same order as they are defined (makes
checking this much easier in the future!). For example bRefresh was missing or some extra/extraLength checks.
InterfaceDescriptor: there is no need for a dump(DeviceHandle) here, so removed it and updated the call in
Interface.java.
2013-06-09 16:36:31 +02:00
Luca Longinotti
bda2fdab54 Use correct call methods, if it returns void: callVoid, else if it returns object: callObject.
Also move JVM reference to be available to all C files (needed for Transfer callback later) and initialize it
once at startup.
2013-06-09 15:16:04 +02:00
Luca Longinotti
1efc33b0f2 Add resetDevice() to also reset the Java Device object after unref, as it really shouldn't be used anymore
since you don't know if the refcount reached zero and it was deallocated. Every new reference means the
refcount gets increased AND a new reference indeed exits, so this always works, unless you let references go
out of scope or overwrite them, which is clearly incorrect in any case.
Also add missing checks for setDeviceHandle() in Transfer.
2013-06-08 00:20:12 +02:00
Luca Longinotti
00670bb48d Implement Transfer callback fully. Transfer cleanup via FREE_TRANSFER also fully guaranteed. 2013-06-07 23:11:09 +02:00
Luca Longinotti
61756ed405 Add basic callback support, still work in progress. 2013-06-07 18:45:53 +02:00
Luca Longinotti
41c92e07c6 Add support for direct allocTransfer(), without isoPackets argument.
Start working on callback support.
2013-06-07 18:24:07 +02:00
Luca Longinotti
817175b686 Only get JVM if not set already (it doesn't change). 2013-06-07 18:09:13 +02:00
Luca Longinotti
8c140fd201 Fix spacing. 2013-06-07 11:52:07 +02:00
Luca Longinotti
66017bfa88 Add new NOT_SET macro to call after NOT_NULL for functions that call set*() functions.
Right now for example, getting a new config descriptor re-using an old ConfigDescriptor object just overwrites the old pointer, without freeing it or telling the user, resulting in a memory leak.
This way, the user gets notified when he's re-using an object that he hasn't cleaned up yet, and he can react and call the appropriate free/cleanup function.
2013-06-07 11:20:58 +02:00
Luca Longinotti
6f542a6228 Separate input checking from function content by a newline, improves readability by a lot!
Add missing NOT_NULL checks.
Simplify getPortPath() ifdef.
Check for result value in libusb_init and only setContext if the call succeeded.
Use always same style of NULL checks in usb4java.h.
2013-06-07 10:51:23 +02:00
Luca Longinotti
1794bbe71d Fix handleEventsTimeoutCompleted and handleEventsCompleted, they require access to a variable representing an integer, that gets set from somewhere outside, like a transfer callback.
The only way to do this in Java is by direct buffers, which IntBuffers can be if allocated correctly, as explained in the JavaDoc.
2013-06-06 15:24:33 +02:00
Luca Longinotti
bcfca40d56 Fix signature comments. 2013-06-06 14:48:35 +02:00
Luca Longinotti
69cecdb21a Fix memory leak: if you malloc, you must also free, JNI never touches manually allocated memory.
Also, I don't see why there is any need to deviate from using a pointer to using a ByteBuffer here, it will not make memory management automatic, and just makes the DeviceDescriptor "different" for no apparent reason. So changed back to pointer.
2013-06-06 14:08:27 +02:00
Luca Longinotti
6d2afe79ac Don't create new scopes in macros, it only makes sense if you have frequent name collisions, and worsens performance.
Change DIRECT_BUFFER to not call the isDirect() method (expensive!), but instead to check the return value of getDirectBufferAddress, which is always used anyway.
It also detects cases where the JVM doesn't support direct buffers and other error conditions this way.
2013-06-06 12:04:50 +02:00
Luca Longinotti
2d5ecef390 Fix signature comments. 2013-06-06 11:32:44 +02:00
Luca Longinotti
36f14cf52d I see now why ints were used here, because all the constants are ints and as such you don't have to do annoying casts and conversions.
I agree with this on set*() calls, but I argue that it is more correct to return the appropriate, smaller type on get*() calls, the same as with the other classes: the values you can read are of appropriate, original size.
2013-06-06 10:36:49 +02:00
Luca Longinotti
f976026e1d Consistently use ByteBuffers in the LibUsb class, getPortPath was the only method that used a byte array, and I personally prefer consistency here, as it gets used like any other function expected to get a char * + length. 2013-06-06 10:29:40 +02:00
Luca Longinotti
d7ce8ce87b We don't need to concern ourselves with length in getStringDescriptorAscii when using a StringBuffer and appending to it.
In C you need to have a length parameter to know how much you can write, here we can always just get the maximum, which is quite small, and then append to the StringBuffer, which will take care of any length management.
2013-06-06 09:59:21 +02:00
Luca Longinotti
a4d5de493b Fix LongBuffer vs IntBuffer usage for getNextTimeout: it has to work with long.
Fix method signatures.
Add TODO wrt wrong usage of completed flag.
2013-06-06 09:28:50 +02:00
Luca Longinotti
b97aa73220 Work on array directly to avoid a full copy. 2013-06-05 18:22:21 +02:00
Luca Longinotti
2ac3763b78 Replace mentions of struct libusb_context with the typedef libusb_context, as is expected by the API.
Also fix init/exit to not handle the default context in any special way: the default context is reference counted, and it is thus NOT an error to call init on it multiple times, or exit for that matter afterwards.
2013-06-05 17:36:42 +02:00
Luca Longinotti
01fa6c971f Fix typo. 2013-06-05 17:36:29 +02:00
Luca Longinotti
2c5ea3c7a4 Set size to zero on reset. Else getting the size is incorrect, and later when checking in get() it would pass the check and you'd access a NULL ptr. 2013-06-05 16:52:24 +02:00
Luca Longinotti
278733f0cb bSynchAddress is a byte in both Java and libusb.h. 2013-06-05 16:49:08 +02:00
Luca Longinotti
164f6e1b36 numAltsetting is an int, in both the Java class and libusb.h. 2013-06-05 16:47:49 +02:00
Luca Longinotti
f022d9ce75 Sync Transfer class with libusb.h definition (byte instead of int mostly).
Add several yet unimplemented methods.
Update test.
2013-06-05 16:45:32 +02:00
Luca Longinotti
3185cb2c61 Rename pointer to more meaningfull names like in the other files. 2013-06-05 15:19:01 +02:00
Luca Longinotti
1a6ca03894 Remove useless extern keywords from definition.
Add missing definitions.
2013-06-05 15:15:49 +02:00
Luca Longinotti
347cb32002 Improve readability.
Don't get same data twice!
2013-06-05 15:14:21 +02:00
Luca Longinotti
5cfed718f4 Fix autotools run. 2013-06-05 15:09:30 +02:00