OpenCores
URL https://opencores.org/ocsvn/usb_fpga_2_13/usb_fpga_2_13/trunk

Subversion Repositories usb_fpga_2_13

[/] [usb_fpga_2_13/] [trunk/] [libusbJava-src/] [ch/] [ntb/] [usb/] [Device.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*
2
 * Java libusb wrapper
3
 * Copyright (c) 2005-2006 Andreas Schläpfer <spandi at users.sourceforge.net>
4
 *
5
 * http://libusbjava.sourceforge.net
6
 * This library is covered by the LGPL, read LGPL.txt for details.
7
 */
8
package ch.ntb.usb;
9
 
10
import java.util.logging.Level;
11
import java.util.logging.Logger;
12
 
13
import ch.ntb.usb.logger.LogUtil;
14
 
15
/**
16
 * This class represents an USB device.<br>
17
 * To get an instance of an USB device use <code>USB.getDevice(...)</code>.
18
 *
19
 */
20
public class Device {
21
 
22
        private static final Logger logger = LogUtil.getLogger("ch.ntb.usb");
23
 
24
        private int maxPacketSize;
25
 
26
        /**
27
         * Mandatory identification values for the device.
28
         */
29
        private int idVendor, idProduct;
30
 
31
        /**
32
         * Optional identification value for the device (e.g. if there are multiple
33
         * devices with the same vendor and product id).
34
         */
35
        private String filename;
36
 
37
        private int dev_configuration, dev_interface, dev_altinterface;
38
 
39
        private long usbDevHandle;
40
 
41
        private boolean resetOnFirstOpen, resetDone;
42
 
43
        private int resetTimeout = 2000;
44
 
45
        private Usb_Device dev;
46
 
47
        protected Device(short idVendor, short idProduct) {
48
                resetOnFirstOpen = false;
49
                resetDone = false;
50
                maxPacketSize = -1;
51
                this.idVendor = idVendor;
52
                this.idProduct = idProduct;
53
                this.filename = null;
54
        }
55
 
56
        protected Device(short idVendor, short idProduct, String filename) {
57
                resetOnFirstOpen = false;
58
                resetDone = false;
59
                maxPacketSize = -1;
60
                this.idVendor = idVendor;
61
                this.idProduct = idProduct;
62
                this.filename = filename;
63
        }
64
 
65
        private void updateMaxPacketSize(Usb_Device device) throws USBException {
66
                maxPacketSize = -1;
67
                Usb_Config_Descriptor[] confDesc = device.getConfig();
68
                for (int i = 0; i < confDesc.length; i++) {
69
                        Usb_Interface[] int_ = confDesc[i].getInterface();
70
                        for (int j = 0; j < int_.length; j++) {
71
                                Usb_Interface_Descriptor[] intDesc = int_[j].getAltsetting();
72
                                for (int k = 0; k < intDesc.length; k++) {
73
                                        Usb_Endpoint_Descriptor[] epDesc = intDesc[k].getEndpoint();
74
                                        for (int l = 0; l < epDesc.length; l++) {
75
                                                maxPacketSize = Math.max(epDesc[l].getWMaxPacketSize(),
76
                                                                maxPacketSize);
77
                                        }
78
                                }
79
                        }
80
                }
81
                if (maxPacketSize <= 0) {
82
                        throw new USBException(
83
                                        "No USB endpoints found. Check the device configuration");
84
                }
85
        }
86
 
87
        /**
88
         * Initializes the device. The parameters <code>idVendor</code> and
89
         * <code>idProduct</code> are mandatory. The parameter
90
         * <code>filename</code> is optional.
91
         */
92
        private Usb_Device initDevice(int idVendorParam, int idProductParam,
93
                        String filename) throws USBException {
94
                Usb_Bus bus = USB.getBus();
95
 
96
                Usb_Device device = null;
97
                // search for device
98
                while (bus != null) {
99
                        device = bus.getDevices();
100
                        while (device != null) {
101
                                Usb_Device_Descriptor devDesc = device.getDescriptor();
102
                                if (filename != null
103
                                                && filename.compareTo(device.getFilename()) == 0
104
                                                && devDesc.getIdVendor() == idVendorParam
105
                                                && devDesc.getIdProduct() == idProductParam) {
106
                                        // idVendor, idProduct and filename
107
                                        logger.info("Device found: " + device.getFilename());
108
                                        updateMaxPacketSize(device);
109
                                        return device;
110
                                } else if (devDesc.getIdVendor() == idVendorParam
111
                                                && devDesc.getIdProduct() == idProductParam) {
112
                                        // only idVendor and idProduct
113
                                        logger.info("Device found: " + device.getFilename());
114
                                        updateMaxPacketSize(device);
115
                                        return device;
116
                                }
117
                                device = device.getNext();
118
                        }
119
                        bus = bus.getNext();
120
                }
121
                return null;
122
        }
123
 
124
        /**
125
         * Updates the device and descriptor information from the bus.<br>
126
         * The descriptors can be read with {@link #getDeviceDescriptor()} and
127
         * {@link #getConfigDescriptors()}.
128
         *
129
         * @throws USBException
130
         */
131
        public void updateDescriptors() throws USBException {
132
                dev = initDevice(idVendor, idProduct, filename);
133
        }
134
 
135
        /**
136
         * Returns the device descriptor associated with this device.<br>
137
         * The descriptor is updated by calling {@link #updateDescriptors()} or
138
         * {@link #open(int, int, int)}.
139
         *
140
         * @return the device descriptor associated with this device or
141
         *         <code>null</code>
142
         */
143
        public Usb_Device_Descriptor getDeviceDescriptor() {
144
                if (dev == null) {
145
                        return null;
146
                }
147
                return dev.getDescriptor();
148
        }
149
 
150
        /**
151
         * Returns the configuration descriptors associated with this device.<br>
152
         * The descriptors are updated by calling {@link #updateDescriptors()} or
153
         * {@link #open(int, int, int)}.
154
         *
155
         * @return the configuration descriptors associated with this device or
156
         *         <code>null</code>
157
         */
158
        public Usb_Config_Descriptor[] getConfigDescriptors() {
159
                if (dev == null) {
160
                        return null;
161
                }
162
                return dev.getConfig();
163
        }
164
 
165
        /**
166
         * Opens the device and claims the specified configuration, interface and
167
         * altinterface.<br>
168
         * First the bus is enumerated. If the device is found its descriptors are
169
         * read and the <code>maxPacketSize</code> value is updated. If no
170
         * endpoints are found in the descriptors an exception is thrown.
171
         *
172
         * @param configuration
173
         *            the configuration, see
174
         *            {@link Usb_Config_Descriptor#getBConfigurationValue()}
175
         * @param interface_
176
         *            the interface, see
177
         *            {@link Usb_Interface_Descriptor#getBInterfaceNumber()}
178
         * @param altinterface
179
         *            the alternate interface, see
180
         *            {@link Usb_Interface_Descriptor#getBAlternateSetting()}. If
181
         *            no alternate interface must be set <i>-1</i> can be used.
182
         * @throws USBException
183
         */
184
        public void open(int configuration, int interface_, int altinterface)
185
                        throws USBException {
186
                this.dev_configuration = configuration;
187
                this.dev_interface = interface_;
188
                this.dev_altinterface = altinterface;
189
 
190
                if (usbDevHandle != 0) {
191
                        throw new USBException("device opened, close or reset first");
192
                }
193
 
194
                dev = initDevice(idVendor, idProduct, filename);
195
 
196
                if (dev != null) {
197
                        long res = LibusbJava.usb_open(dev);
198
                        if (res == 0) {
199
                                throw new USBException("LibusbJava.usb_open: "
200
                                                + LibusbJava.usb_strerror());
201
                        }
202
                        usbDevHandle = res;
203
                }
204
 
205
                if (dev == null || usbDevHandle == 0) {
206
                        throw new USBException("USB device with idVendor 0x"
207
                                        + Integer.toHexString(idVendor & 0xFFFF)
208
                                        + " and idProduct 0x"
209
                                        + Integer.toHexString(idProduct & 0xFFFF) + " not found");
210
                }
211
                claim_interface(usbDevHandle, configuration, interface_, altinterface);
212
                if (resetOnFirstOpen & !resetDone) {
213
                        logger.info("reset on first open");
214
                        resetDone = true;
215
                        reset();
216
                        try {
217
                                Thread.sleep(resetTimeout);
218
                        } catch (InterruptedException e) {
219
                                //
220
                        }
221
                        open(configuration, interface_, altinterface);
222
                }
223
        }
224
 
225
        /**
226
         * Release the claimed interface and close the opened device.<br>
227
         *
228
         * @throws USBException
229
         */
230
        public void close() throws USBException {
231
                if (usbDevHandle == 0) {
232
                        throw new USBException("invalid device handle");
233
                }
234
                release_interface(usbDevHandle, dev_interface);
235
                if (LibusbJava.usb_close(usbDevHandle) < 0) {
236
                        usbDevHandle = 0;
237
                        throw new USBException("LibusbJava.usb_close: "
238
                                        + LibusbJava.usb_strerror());
239
                }
240
                usbDevHandle = 0;
241
                maxPacketSize = -1;
242
                logger.info("device closed");
243
        }
244
 
245
        /**
246
         * Sends an USB reset to the device. The device handle will no longer be
247
         * valid. To use the device again, {@link #open(int, int, int)} must be
248
         * called.
249
         *
250
         * @throws USBException
251
         */
252
        public void reset() throws USBException {
253
                if (usbDevHandle == 0) {
254
                        throw new USBException("invalid device handle");
255
                }
256
                release_interface(usbDevHandle, dev_interface);
257
                if (LibusbJava.usb_reset(usbDevHandle) < 0) {
258
                        usbDevHandle = 0;
259
                        throw new USBException("LibusbJava.usb_reset: "
260
                                        + LibusbJava.usb_strerror());
261
                }
262
                usbDevHandle = 0;
263
                logger.info("device reset");
264
        }
265
 
266
        /**
267
         * Write data to the device using a bulk transfer.<br>
268
         *
269
         * @param out_ep_address
270
         *            endpoint address to write to
271
         * @param data
272
         *            data to write to this endpoint
273
         * @param size
274
         *            size of the data
275
         * @param timeout
276
         *            amount of time in ms the device will try to send the data
277
         *            until a timeout exception is thrown
278
         * @param reopenOnTimeout
279
         *            if set to true, the device will try to open the connection and
280
         *            send the data again before a timeout exception is thrown
281
         * @return the actual number of bytes written
282
         * @throws USBException
283
         */
284
        public int writeBulk(int out_ep_address, byte[] data, int size,
285
                        int timeout, boolean reopenOnTimeout) throws USBException {
286
                if (usbDevHandle == 0) {
287
                        throw new USBException("invalid device handle");
288
                }
289
                if (data == null) {
290
                        throw new USBException("data must not be null");
291
                }
292
                if (size <= 0 || size > data.length) {
293
                        throw new ArrayIndexOutOfBoundsException("invalid size: " + size);
294
                }
295
                int lenWritten = LibusbJava.usb_bulk_write(usbDevHandle,
296
                                out_ep_address, data, size, timeout);
297
                if (lenWritten < 0) {
298
                        if (lenWritten == LibusbJava.ERROR_TIMEDOUT) {
299
                                // try to reopen the device and send the data again
300
                                if (reopenOnTimeout) {
301
                                        logger.info("try to reopen");
302
                                        reset();
303
                                        open(dev_configuration, dev_interface, dev_altinterface);
304
                                        return writeBulk(out_ep_address, data, size, timeout, false);
305
                                }
306
                                throw new USBTimeoutException("LibusbJava.usb_bulk_write: "
307
                                                + LibusbJava.usb_strerror());
308
                        }
309
                        throw new USBException("LibusbJava.usb_bulk_write: "
310
                                        + LibusbJava.usb_strerror());
311
                }
312
 
313
                logger.info("length written: " + lenWritten);
314
                if (logger.isLoggable(Level.FINEST)) {
315
                        StringBuffer sb = new StringBuffer("bulkwrite, ep 0x"
316
                                        + Integer.toHexString(out_ep_address) + ": " + lenWritten
317
                                        + " Bytes sent: ");
318
                        for (int i = 0; i < lenWritten; i++) {
319
                                sb.append("0x" + String.format("%1$02X", data[i]) + " ");
320
                        }
321
                        logger.info(sb.toString());
322
                }
323
                return lenWritten;
324
        }
325
 
326
        /**
327
         * Read data from the device using a bulk transfer.<br>
328
         *
329
         * @param in_ep_address
330
         *            endpoint address to read from
331
         * @param data
332
         *            data buffer for the data to be read
333
         * @param size
334
         *            the maximum requested data size
335
         * @param timeout
336
         *            amount of time in ms the device will try to receive data until
337
         *            a timeout exception is thrown
338
         * @param reopenOnTimeout
339
         *            if set to true, the device will try to open the connection and
340
         *            receive the data again before a timeout exception is thrown
341
         * @return the actual number of bytes read
342
         * @throws USBException
343
         */
344
        public int readBulk(int in_ep_address, byte[] data, int size, int timeout,
345
                        boolean reopenOnTimeout) throws USBException {
346
                if (usbDevHandle == 0) {
347
                        throw new USBException("invalid device handle");
348
                }
349
                if (data == null) {
350
                        throw new USBException("data must not be null");
351
                }
352
                if (size <= 0 || size > data.length) {
353
                        throw new ArrayIndexOutOfBoundsException("invalid size: " + size);
354
                }
355
                int lenRead = LibusbJava.usb_bulk_read(usbDevHandle, in_ep_address,
356
                                data, size, timeout);
357
                if (lenRead < 0) {
358
                        if (lenRead == LibusbJava.ERROR_TIMEDOUT) {
359
                                // try to reopen the device and send the data again
360
                                if (reopenOnTimeout) {
361
                                        logger.info("try to reopen");
362
                                        reset();
363
                                        open(dev_configuration, dev_interface, dev_altinterface);
364
                                        return readBulk(in_ep_address, data, size, timeout, false);
365
                                }
366
                                throw new USBTimeoutException("LibusbJava.usb_bulk_read: "
367
                                                + LibusbJava.usb_strerror());
368
                        }
369
                        throw new USBException("LibusbJava.usb_bulk_read: "
370
                                        + LibusbJava.usb_strerror());
371
                }
372
 
373
                logger.info("length read: " + lenRead);
374
                if (logger.isLoggable(Level.FINEST)) {
375
                        StringBuffer sb = new StringBuffer("bulkread, ep 0x"
376
                                        + Integer.toHexString(in_ep_address) + ": " + lenRead
377
                                        + " Bytes received: ");
378
                        for (int i = 0; i < lenRead; i++) {
379
                                sb.append("0x" + String.format("%1$02X", data[i]) + " ");
380
                        }
381
                        logger.info(sb.toString());
382
                }
383
                return lenRead;
384
        }
385
 
386
        /**
387
         * Write data to the device using a interrupt transfer.<br>
388
         *
389
         * @param out_ep_address
390
         *            endpoint address to write to
391
         * @param data
392
         *            data to write to this endpoint
393
         * @param size
394
         *            size of the data
395
         * @param timeout
396
         *            amount of time in ms the device will try to send the data
397
         *            until a timeout exception is thrown
398
         * @param reopenOnTimeout
399
         *            if set to true, the device will try to open the connection and
400
         *            send the data again before a timeout exception is thrown
401
         * @return the actual number of bytes written
402
         * @throws USBException
403
         */
404
        public int writeInterrupt(int out_ep_address, byte[] data, int size,
405
                        int timeout, boolean reopenOnTimeout) throws USBException {
406
                if (usbDevHandle == 0) {
407
                        throw new USBException("invalid device handle");
408
                }
409
                if (data == null) {
410
                        throw new USBException("data must not be null");
411
                }
412
                if (size <= 0 || size > data.length) {
413
                        throw new ArrayIndexOutOfBoundsException("invalid size: " + size);
414
                }
415
                int lenWritten = LibusbJava.usb_interrupt_write(usbDevHandle,
416
                                out_ep_address, data, size, timeout);
417
                if (lenWritten < 0) {
418
                        if (lenWritten == LibusbJava.ERROR_TIMEDOUT) {
419
                                // try to reopen the device and send the data again
420
                                if (reopenOnTimeout) {
421
                                        logger.info("try to reopen");
422
                                        reset();
423
                                        open(dev_configuration, dev_interface, dev_altinterface);
424
                                        return writeInterrupt(out_ep_address, data, size, timeout,
425
                                                        false);
426
                                }
427
                                throw new USBTimeoutException(
428
                                                "LibusbJava.usb_interrupt_write: "
429
                                                                + LibusbJava.usb_strerror());
430
                        }
431
                        throw new USBException("LibusbJava.usb_interrupt_write: "
432
                                        + LibusbJava.usb_strerror());
433
                }
434
 
435
                logger.info("length written: " + lenWritten);
436
                if (logger.isLoggable(Level.FINEST)) {
437
                        StringBuffer sb = new StringBuffer("interruptwrite, ep 0x"
438
                                        + Integer.toHexString(out_ep_address) + ": " + lenWritten
439
                                        + " Bytes sent: ");
440
                        for (int i = 0; i < lenWritten; i++) {
441
                                sb.append("0x" + String.format("%1$02X", data[i]) + " ");
442
                        }
443
                        logger.info(sb.toString());
444
                }
445
                return lenWritten;
446
        }
447
 
448
        /**
449
         * Read data from the device using a interrupt transfer.<br>
450
         *
451
         * @param in_ep_address
452
         *            endpoint address to read from
453
         * @param data
454
         *            data buffer for the data to be read
455
         * @param size
456
         *            the maximum requested data size
457
         * @param timeout
458
         *            amount of time in ms the device will try to receive data until
459
         *            a timeout exception is thrown
460
         * @param reopenOnTimeout
461
         *            if set to true, the device will try to open the connection and
462
         *            receive the data again before a timeout exception is thrown
463
         * @return the actual number of bytes read
464
         * @throws USBException
465
         */
466
        public int readInterrupt(int in_ep_address, byte[] data, int size,
467
                        int timeout, boolean reopenOnTimeout) throws USBException {
468
                if (usbDevHandle == 0) {
469
                        throw new USBException("invalid device handle");
470
                }
471
                if (data == null) {
472
                        throw new USBException("data must not be null");
473
                }
474
                if (size <= 0 || size > data.length) {
475
                        throw new ArrayIndexOutOfBoundsException("invalid size: " + size);
476
                }
477
                int lenRead = LibusbJava.usb_interrupt_read(usbDevHandle,
478
                                in_ep_address, data, size, timeout);
479
                if (lenRead < 0) {
480
                        if (lenRead == LibusbJava.ERROR_TIMEDOUT) {
481
                                // try to reopen the device and send the data again
482
                                if (reopenOnTimeout) {
483
                                        logger.info("try to reopen");
484
                                        reset();
485
                                        open(dev_configuration, dev_interface, dev_altinterface);
486
                                        return readInterrupt(in_ep_address, data, size, timeout,
487
                                                        false);
488
                                }
489
                                throw new USBTimeoutException("LibusbJava.usb_interrupt_read: "
490
                                                + LibusbJava.usb_strerror());
491
                        }
492
                        throw new USBException("LibusbJava.usb_interrupt_read: "
493
                                        + LibusbJava.usb_strerror());
494
                }
495
 
496
                logger.info("length read: " + lenRead);
497
                if (logger.isLoggable(Level.FINEST)) {
498
                        StringBuffer sb = new StringBuffer("interrupt, ep 0x"
499
                                        + Integer.toHexString(in_ep_address) + ": " + lenRead
500
                                        + " Bytes received: ");
501
                        for (int i = 0; i < lenRead; i++) {
502
                                sb.append("0x" + String.format("%1$02X", data[i]) + " ");
503
                        }
504
                        logger.info(sb.toString());
505
                }
506
                return lenRead;
507
        }
508
 
509
        /**
510
         * Performs a control request to the default control pipe on a device.<br>
511
         * The parameters mirror the types of the same name in the USB
512
         * specification.
513
         *
514
         * @param requestType
515
         *            USB device request type (USB specification 9.3,
516
         *            bmRequestType). Use constants from {@link ch.ntb.usb.USB}
517
         *            (REQ_TYPE_xxx).
518
         * @param request
519
         *            specific request (USB specification 9.4, bRequest). Use
520
         *            constants from {@link ch.ntb.usb.USB} (REQ_xxx).
521
         * @param value
522
         *            field that varies according to request (USB specification 9.4,
523
         *            wValue)
524
         * @param index
525
         *            field that varies according to request (USB specification 9.4,
526
         *            wIndex)
527
         * @param data
528
         *            the send/receive buffer
529
         * @param size
530
         *            the buffer size. 0 is a valid value, but there must still be a
531
         *            dummy data buffer provided.
532
         * @param timeout
533
         *            amount of time in ms the device will try to send/receive data
534
         *            until a timeout exception is thrown
535
         * @param reopenOnTimeout
536
         *            if set to true, the device will try to open the connection and
537
         *            send/receive the data again before a timeout exception is
538
         *            thrown
539
         * @return the number of bytes written/read
540
         * @throws USBException
541
         */
542
        public int controlMsg(int requestType, int request, int value, int index,
543
                        byte[] data, int size, int timeout, boolean reopenOnTimeout)
544
                        throws USBException {
545
                if (usbDevHandle == 0) {
546
                        throw new USBException("invalid device handle");
547
                }
548
                if (data == null) {
549
                        throw new USBException("data must not be null");
550
                }
551
                if (size < 0 || size > data.length) {
552
                        throw new ArrayIndexOutOfBoundsException("invalid size: " + size);
553
                }
554
                int len = LibusbJava.usb_control_msg(usbDevHandle, requestType,
555
                                request, value, index, data, size, timeout);
556
                if (len < 0) {
557
                        if (len == LibusbJava.ERROR_TIMEDOUT) {
558
                                // try to reopen the device and send the data again
559
                                if (reopenOnTimeout) {
560
                                        logger.info("try to reopen");
561
                                        reset();
562
                                        open(dev_configuration, dev_interface, dev_altinterface);
563
                                        return controlMsg(requestType, request, value, index, data,
564
                                                        size, timeout, false);
565
                                }
566
                                throw new USBTimeoutException("LibusbJava.controlMsg: "
567
                                                + LibusbJava.usb_strerror());
568
                        }
569
                        throw new USBException("LibusbJava.controlMsg: "
570
                                        + LibusbJava.usb_strerror());
571
                }
572
 
573
                logger.info("length read/written: " + len);
574
                if (logger.isLoggable(Level.FINEST)) {
575
                        StringBuffer sb = new StringBuffer("controlMsg: " + len
576
                                        + " Bytes received(written: ");
577
                        for (int i = 0; i < len; i++) {
578
                                sb.append("0x" + String.format("%1$02X", data[i]) + " ");
579
                        }
580
                        logger.info(sb.toString());
581
                }
582
                return len;
583
        }
584
 
585
        /**
586
         * Claim an interface to send and receive USB data.<br>
587
         *
588
         * @param usb_dev_handle
589
         *            the handle of the device <b>(MUST BE VALID)</b>
590
         * @param configuration
591
         *            the configuration to use
592
         * @param interface_
593
         *            the interface to claim
594
         * @param altinterface
595
         *            the alternate interface to use. If no alternate interface must
596
         *            be set <i>-1</i> can be used.
597
         * @throws USBException
598
         *             throws an USBException if the action fails
599
         */
600
        private void claim_interface(long usb_dev_handle, int configuration,
601
                        int interface_, int altinterface) throws USBException {
602
                if (LibusbJava.usb_set_configuration(usb_dev_handle, configuration) < 0) {
603
                        usbDevHandle = 0;
604
                        throw new USBException("LibusbJava.usb_set_configuration: "
605
                                        + LibusbJava.usb_strerror());
606
                }
607
                if (LibusbJava.usb_claim_interface(usb_dev_handle, interface_) < 0) {
608
                        usbDevHandle = 0;
609
                        throw new USBException("LibusbJava.usb_claim_interface: "
610
                                        + LibusbJava.usb_strerror());
611
                }
612
                if (altinterface >= 0) {
613
                        if (LibusbJava.usb_set_altinterface(usb_dev_handle, altinterface) < 0) {
614
                                try {
615
                                        release_interface(usb_dev_handle, interface_);
616
                                } catch (USBException e) {
617
                                        // ignore
618
                                }
619
                                usbDevHandle = 0;
620
                                throw new USBException("LibusbJava.usb_set_altinterface: "
621
                                                + LibusbJava.usb_strerror());
622
                        }
623
                }
624
                logger.info("interface claimed");
625
        }
626
 
627
        /**
628
         * Release a previously claimed interface.<br>
629
         *
630
         * @param dev_handle
631
         *            the handle of the device <b>(MUST BE VALID)</b>
632
         * @param interface_
633
         *            the interface to claim
634
         * @throws USBException
635
         *             throws an USBException if the action fails
636
         */
637
        private void release_interface(long dev_handle, int interface_)
638
                        throws USBException {
639
                if (LibusbJava.usb_release_interface(dev_handle, interface_) < 0) {
640
                        usbDevHandle = 0;
641
                        throw new USBException("LibusbJava.usb_release_interface: "
642
                                        + LibusbJava.usb_strerror());
643
                }
644
                logger.info("interface released");
645
        }
646
 
647
        /**
648
         * Returns the product ID of the device.<br>
649
         *
650
         * @return the product ID of the device.
651
         */
652
        public int getIdProduct() {
653
                return idProduct;
654
        }
655
 
656
        /**
657
         * Returns the vendor ID of the device.<br>
658
         *
659
         * @return the vendor ID of the device.
660
         */
661
        public int getIdVendor() {
662
                return idVendor;
663
        }
664
 
665
        /**
666
         * Returns the alternative interface.<br>
667
         * This value is only valid after opening the device.
668
         *
669
         * @return the alternative interface. This value is only valid after opening
670
         *         the device.
671
         */
672
        public int getAltinterface() {
673
                return dev_altinterface;
674
        }
675
 
676
        /**
677
         * Returns the current configuration used.<br>
678
         * This value is only valid after opening the device.
679
         *
680
         * @return the current configuration used. This value is only valid after
681
         *         opening the device.
682
         */
683
        public int getConfiguration() {
684
                return dev_configuration;
685
        }
686
 
687
        /**
688
         * Returns the current interface.<br>
689
         * This value is only valid after opening the device.
690
         *
691
         * @return the current interface. This value is only valid after opening the
692
         *         device.
693
         */
694
        public int getInterface() {
695
                return dev_interface;
696
        }
697
 
698
        /**
699
         * Returns the maximum packet size in bytes which is allowed to be
700
         * transmitted at once.<br>
701
         * The value is determined by reading the endpoint descriptor(s) when
702
         * opening the device. It is invalid before the device is opened! Note that
703
         * if some endpoints use different packet sizes the maximum packet size is
704
         * return. This value may be used to determine if a device is opened in
705
         * fullspeed or highspeed mode.
706
         *
707
         * @return the maximum packet size
708
         */
709
        public int getMaxPacketSize() {
710
                return maxPacketSize;
711
        }
712
 
713
        /**
714
         * Check if the device is open.<br>
715
         * This checks only for a valid device handle. It doesn't check if the
716
         * device is still attached or working.
717
         *
718
         * @return true if the device is open
719
         */
720
        public boolean isOpen() {
721
                return usbDevHandle != 0;
722
        }
723
 
724
        /**
725
         * If enabled, the device is reset when first opened. <br>
726
         * This will only happen once. When the application is started, the device
727
         * state is unknown. If the device is not reset, read or write may result in
728
         * a {@link USBTimeoutException}.<br>
729
         * <br>
730
         * This feature is disabled by default.
731
         *
732
         * @param enable
733
         *            true if the device should be reset when first opened
734
         * @param timeout
735
         *            the timeout between the reset and the reopening
736
         */
737
        public void setResetOnFirstOpen(boolean enable, int timeout) {
738
                resetOnFirstOpen = enable;
739
                resetTimeout = timeout;
740
        }
741
 
742
        /**
743
         * Returns the optional filename which is set when there are multiple
744
         * devices with the same vendor and product id. See
745
         * {@link USB#getDevice(short, short, String)}. Use
746
         * {@link Usb_Device#getFilename()} to read the filename of a device.
747
         *
748
         * @return the filename if set or null
749
         */
750
        protected String getFilename() {
751
                return filename;
752
        }
753
 
754
        /**
755
         * Returns the Usb_Device instance associated with this device. This value
756
         * is only valid after opening the device.
757
         *
758
         * @return the Usb_Device instance associated with this device.
759
         */
760
        public Usb_Device getDevice() {
761
                return dev;
762
        }
763
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.