-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathTransfer.java
More file actions
347 lines (309 loc) · 8.96 KB
/
Transfer.java
File metadata and controls
347 lines (309 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Copyright 2013 Klaus Reimer <[email protected]>
* Copyright 2013 Luca Longinotti <[email protected]>
* See LICENSE.md for licensing information.
*
* Based on libusb <http://libusb.info/>:
*
* Copyright 2001 Johannes Erdfelt <[email protected]>
* Copyright 2007-2009 Daniel Drake <[email protected]>
* Copyright 2010-2012 Peter Stuge <[email protected]>
* Copyright 2008-2013 Nathan Hjelm <[email protected]>
* Copyright 2009-2013 Pete Batard <[email protected]>
* Copyright 2009-2013 Ludovic Rousseau <[email protected]>
* Copyright 2010-2012 Michael Plante <[email protected]>
* Copyright 2011-2013 Hans de Goede <[email protected]>
* Copyright 2012-2013 Martin Pieuchot <[email protected]>
* Copyright 2012-2013 Toby Gray <[email protected]>
*/
package org.usb4java;
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.
*
* @author Klaus Reimer ([email protected])
*/
public final class Transfer
{
/** The native pointer to the transfer structure. */
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;
/**
* Package-private constructor to prevent manual instantiation.
* Transfers are always created by JNI with allocTransfer().
*/
Transfer()
{
// Empty
}
/**
* Returns the native pointer.
*
* @return The native pointer.
*/
public long getPointer()
{
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 devHandle();
/**
* Sets the handle of the device that this transfer will be submitted to.
*
* @param handle
* The handle of the device.
*/
public native void setDevHandle(final DeviceHandle handle);
/**
* Returns the bitwise OR combination of libusb transfer flags.
*
* @return The transfer flags.
*/
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 byte flags);
/**
* Returns the address of the endpoint where this transfer will be sent.
*
* @return The endpoint address.
*/
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 byte endpoint);
/**
* Returns the type of the endpoint.
*
* @return The endpoint type.
*/
public native byte type();
/**
* Sets the type of the endpoint.
*
* @param type
* The endpoint type to set.
*/
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 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 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 status();
/**
* Returns the length of the data buffer.
*
* @return The data buffer length.
*/
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 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 method called internally to set the length of the data buffer.
*
* @param length
* The length to set.
*/
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 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 ByteBuffer buffer()
{
return this.transferBuffer;
}
/**
* Sets the data buffer.
*
* @param buffer
* The data buffer to set.
*/
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 method called internally to set the data buffer.
*
* @param buffer
* The data buffer to set.
*/
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 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);
}
}