1 |
2 |
ZTEX |
/*!
|
2 |
3 |
ZTEX |
Java host software API of ZTEX SDK
|
3 |
|
|
Copyright (C) 2009-2014 ZTEX GmbH.
|
4 |
2 |
ZTEX |
http://www.ztex.de
|
5 |
|
|
|
6 |
|
|
This program is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License version 3 as
|
8 |
|
|
published by the Free Software Foundation.
|
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, but
|
11 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 |
|
|
General Public License for more details.
|
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License
|
16 |
|
|
along with this program; if not, see http://www.gnu.org/licenses/.
|
17 |
|
|
!*/
|
18 |
|
|
|
19 |
|
|
/*
|
20 |
|
|
Scan bus for devices with ZTEX descriptor 1 and/or Cypress EZ-USB FX2 devices
|
21 |
|
|
*/
|
22 |
|
|
package ztex;
|
23 |
|
|
|
24 |
|
|
import java.io.*;
|
25 |
|
|
import java.util.*;
|
26 |
|
|
|
27 |
|
|
import ch.ntb.usb.*;
|
28 |
|
|
|
29 |
|
|
/**
|
30 |
|
|
* A class used for finding the EZ-USB devices on the USB.
|
31 |
|
|
* The devices found are stored as a list of {@link ZtexDevice1} instances.
|
32 |
|
|
* @see ZtexDevice1
|
33 |
|
|
*/
|
34 |
|
|
|
35 |
|
|
public class ZtexScanBus1 {
|
36 |
|
|
private Vector<ZtexDevice1> devices = new Vector<ZtexDevice1>();
|
37 |
|
|
|
38 |
|
|
// ******* ZtexScanBus1 ********************************************************
|
39 |
|
|
/**
|
40 |
|
|
* Scans the USB for suitable devices and constructs a list of them.
|
41 |
|
|
* Four kinds of search filters can be applied
|
42 |
|
|
* <ol>
|
43 |
|
|
* <li> usbVendorId and usbProductId can be used to search for devices with a given vendor and product ID. These devices must provide a ZTEX descriptor 1.</li>
|
44 |
|
|
* <li> If a certain interface version is required, it can be specified using interfaceVersion. </li>
|
45 |
|
|
* <li> Incompatible devices can be excluded by the specification of the ZTEX product ID's, see {@link ZtexDevice1#compatible(int,int,int,int)}. </li>
|
46 |
|
|
* <li> If scanUnconfigured is true, also devices without ZTEX Firmware and devices with Cypress EZ-USB USB are considered</li>
|
47 |
|
|
* <li> In multi device environment a single device can be selected by giving a serial number. </li>
|
48 |
|
|
* </ol>
|
49 |
|
|
* @param usbVendorId USB vendor ID of the device to be searched for
|
50 |
|
|
* @param usbProductId USB product ID of the device to be searched for
|
51 |
|
|
* @param scanUnconfigured if true, scan for unconfigured devices and devices with Cypress EZ-USB USB ID's
|
52 |
|
|
* @param quiet if true, don't print any warnings
|
53 |
|
|
* @param interfaceVersion The required interface version (<0 if no interface version is required)
|
54 |
|
|
* @param snString The serial number of the device
|
55 |
|
|
* @param productId0 Byte 0 of a given ZTEX product ID (≤0 if not to be considered)
|
56 |
|
|
* @param productId1 Byte 1 of a given ZTEX product ID (≤0 if not to be considered)
|
57 |
|
|
* @param productId2 Byte 2 of a given ZTEX product ID (≤0 if not to be considered)
|
58 |
|
|
* @param productId3 Byte 3 of a given ZTEX product ID (≤0 if not to be considered)
|
59 |
|
|
*/
|
60 |
|
|
public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanUnconfigured, boolean quiet, int interfaceVersion, String snString, int productId0, int productId1, int productId2, int productId3 ) {
|
61 |
|
|
LibusbJava.usb_find_busses();
|
62 |
|
|
LibusbJava.usb_find_devices();
|
63 |
|
|
|
64 |
|
|
Usb_Bus bus = LibusbJava.usb_get_busses();
|
65 |
|
|
|
66 |
|
|
while ( bus != null ) {
|
67 |
|
|
Usb_Device dev = bus.getDevices();
|
68 |
|
|
while ( dev != null ) {
|
69 |
|
|
try {
|
70 |
|
|
ZtexDevice1 zdev = new ZtexDevice1( dev, usbVendorId, usbProductId, scanUnconfigured );
|
71 |
|
|
if ( scanUnconfigured ||
|
72 |
|
|
( zdev.valid() &&
|
73 |
|
|
( interfaceVersion<0 || zdev.interfaceVersion()==interfaceVersion ) &&
|
74 |
|
|
( snString == null || zdev.snString().equals(snString) ) &&
|
75 |
|
|
zdev.compatible(productId0, productId1, productId2, productId3)
|
76 |
|
|
)
|
77 |
|
|
)
|
78 |
|
|
devices.add( zdev );
|
79 |
|
|
}
|
80 |
|
|
catch ( DeviceNotSupportedException e ) {
|
81 |
|
|
}
|
82 |
|
|
catch ( Exception e ) {
|
83 |
|
|
if ( ! quiet )
|
84 |
|
|
System.err.println( e.getLocalizedMessage() );
|
85 |
|
|
}
|
86 |
|
|
dev = dev.getNext();
|
87 |
|
|
}
|
88 |
|
|
bus = bus.getNext();
|
89 |
|
|
}
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
/**
|
93 |
|
|
* Scans the USB for suitable devices and constructs a list of them.
|
94 |
|
|
* Three kinds of search filters can be applied
|
95 |
|
|
* <ol>
|
96 |
|
|
* <li> usbVendorId and usbProductId can be used to search for devices with a given vendor and product ID. These devices must provide a ZTEX descriptor 1.</li>
|
97 |
|
|
* <li> If a certain interface version is required, it can be specified using interfaceVersion. </li>
|
98 |
|
|
* <li> If scanUnconfigured is true, also devices without ZTEX Firmware and devices with Cypress EZ-USB USB are considered</li>
|
99 |
|
|
* <li> In multi device environment a single device can be selected by giving a serial number. </li>
|
100 |
|
|
* </ol>
|
101 |
|
|
* @param usbVendorId USB vendor ID of the device to be searched for
|
102 |
|
|
* @param usbProductId USB product ID of the device to be searched for
|
103 |
|
|
* @param scanUnconfigured if true, scan for unconfigured devices and devices with Cypress EZ-USB USB ID's
|
104 |
|
|
* @param quiet if true, don't print any warnings
|
105 |
|
|
* @param interfaceVersion The required interface version (<0 if no interface version is required)
|
106 |
|
|
*/
|
107 |
|
|
public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanUnconfigured, boolean quiet, int interfaceVersion, String snString ) {
|
108 |
|
|
this(usbVendorId, usbProductId, scanUnconfigured, quiet, interfaceVersion, snString, -1,-1,-1,-1 );
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
/**
|
112 |
|
|
* Scans the USB for suitable devices and constructs a list of them.
|
113 |
|
|
* Three kinds of search filters can be applied
|
114 |
|
|
* <ol>
|
115 |
|
|
* <li> usbVendorId and usbProductId can be used to search for devices with a given vendor and product ID. These devices must provide a ZTEX descriptor 1.</li>
|
116 |
|
|
* <li> If a certain interface version is required, it can be specified using interfaceVersion. </li>
|
117 |
|
|
* <li> If scanUnconfigured is true, also devices without ZTEX Firmware and devices with Cypress EZ-USB USB are considered</li>
|
118 |
|
|
* </ol>
|
119 |
|
|
* @param usbVendorId USB vendor ID of the device to be searched for
|
120 |
|
|
* @param usbProductId USB product ID of the device to be searched for
|
121 |
|
|
* @param scanUnconfigured if true, scan for unconfigured devices and devices with Cypress EZ-USB USB ID's
|
122 |
|
|
* @param quiet if true, don't print any warnings
|
123 |
|
|
* @param interfaceVersion The required interface version (<0 if no interface version is required)
|
124 |
|
|
*/
|
125 |
|
|
public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanUnconfigured, boolean quiet, int interfaceVersion ) {
|
126 |
|
|
this(usbVendorId, usbProductId, scanUnconfigured, quiet, interfaceVersion, null, -1,-1,-1,-1 );
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
/**
|
130 |
|
|
* Scans the USB for suitable devices and constructs a list of them.
|
131 |
|
|
* Two kinds of search filters can be applied
|
132 |
|
|
* <ol>
|
133 |
|
|
* <li> usbVendorId and usbProductId can be used to search for devices with a given vendor and product ID. These devices must provide a ZTEX descriptor 1.</li>
|
134 |
|
|
* <li> If scanUnconfigured is true, also devices without ZTEX Firmware and devices with Cypress EZ-USB USB are considered</li>
|
135 |
|
|
* </ol>
|
136 |
|
|
* @param usbVendorId USB vendor ID of the device to be searched for
|
137 |
|
|
* @param usbProductId USB product ID of the device to be searched for
|
138 |
|
|
* @param scanUnconfigured if true, scan for unconfigured devices and devices with Cypress EZ-USB USB ID's
|
139 |
|
|
* @param quiet if true, don't print any warnings
|
140 |
|
|
*/
|
141 |
|
|
public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanUnconfigured, boolean quiet ) {
|
142 |
|
|
this(usbVendorId, usbProductId, scanUnconfigured, quiet, -1, null, -1,-1,-1,-1 );
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
// ******* printBus ************************************************************
|
146 |
|
|
/**
|
147 |
|
|
* Prints out a list of devices found.
|
148 |
|
|
* @param out Where the output is to be printed to.
|
149 |
|
|
*/
|
150 |
|
|
public void printBus( PrintStream out ) {
|
151 |
|
|
for (int i=0; i<devices.size(); i++ ) {
|
152 |
|
|
out.println( i + ": " + devices.elementAt(i).toString() );
|
153 |
|
|
}
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
// ******* numberOfDevices *****************************************************
|
157 |
|
|
/**
|
158 |
|
|
* Returns the number of devices found.
|
159 |
|
|
* @return the number of devices found.
|
160 |
|
|
*/
|
161 |
|
|
public final int numberOfDevices () {
|
162 |
|
|
return devices.size();
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
// ******* device **************************************************************
|
166 |
|
|
/**
|
167 |
|
|
* Returns a device from the list of devices.
|
168 |
|
|
* @param i The device index.
|
169 |
|
|
* @return a device from the list of devices.
|
170 |
|
|
* @throws IndexOutOfBoundsException if i<0 or i≥{@link #numberOfDevices()}
|
171 |
|
|
*/
|
172 |
|
|
public final ZtexDevice1 device (int i) throws IndexOutOfBoundsException {
|
173 |
|
|
if ( i<0 || i>=devices.size() )
|
174 |
|
|
throw new IndexOutOfBoundsException( "Device number out of range. Valid numbers are 0.." + (devices.size()-1) );
|
175 |
|
|
return devices.elementAt(i);
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
}
|
179 |
|
|
|