1 |
2 |
ZTEX |
/*%
|
2 |
|
|
Java host software API of ZTEX SDK
|
3 |
|
|
Copyright (C) 2009-2017 ZTEX GmbH.
|
4 |
|
|
http://www.ztex.de
|
5 |
|
|
|
6 |
|
|
This Source Code Form is subject to the terms of the Mozilla Public
|
7 |
|
|
License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
8 |
|
|
You can obtain one at http://mozilla.org/MPL/2.0/.
|
9 |
|
|
|
10 |
|
|
Alternatively, the contents of this file may be used under the terms
|
11 |
|
|
of the GNU General Public License Version 3, as described below:
|
12 |
|
|
|
13 |
|
|
This program is free software; you can redistribute it and/or modify
|
14 |
|
|
it under the terms of the GNU General Public License version 3 as
|
15 |
|
|
published by the Free Software Foundation.
|
16 |
|
|
|
17 |
|
|
This program is distributed in the hope that it will be useful, but
|
18 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
20 |
|
|
General Public License for more details.
|
21 |
|
|
|
22 |
|
|
You should have received a copy of the GNU General Public License
|
23 |
|
|
along with this program; if not, see http://www.gnu.org/licenses/.
|
24 |
|
|
%*/
|
25 |
|
|
|
26 |
|
|
/*
|
27 |
|
|
Scan bus for devices with ZTEX descriptor 1 and/or Cypress EZ-USB FX2 devices
|
28 |
|
|
*/
|
29 |
|
|
package ztex;
|
30 |
|
|
|
31 |
|
|
import java.io.*;
|
32 |
|
|
import java.util.*;
|
33 |
|
|
|
34 |
|
|
import org.usb4java.*;
|
35 |
|
|
|
36 |
|
|
/**
|
37 |
|
|
* A class for scanning the USB for EZ-USB devices.
|
38 |
|
|
* The devices found are stored as a list of {@link ZtexDevice1} instances.
|
39 |
|
|
* @see ZtexDevice1
|
40 |
|
|
*/
|
41 |
|
|
|
42 |
|
|
public class ZtexScanBus1 {
|
43 |
|
|
private Vector<ZtexDevice1> devices = new Vector<ZtexDevice1>();
|
44 |
|
|
private int refCount = 0;
|
45 |
|
|
|
46 |
|
|
// ******* ZtexScanBus1 ********************************************************
|
47 |
|
|
/**
|
48 |
|
|
* Scans the USB for suitable devices and constructs a list of them.
|
49 |
|
|
* Four kinds of search filters can be applied
|
50 |
|
|
* <ol>
|
51 |
|
|
* <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>
|
52 |
|
|
* <li> If a certain interface version is required, it can be specified using interfaceVersion. </li>
|
53 |
|
|
* <li> Incompatible devices can be excluded by the specification of the ZTEX product ID's, see {@link ZtexDevice1#compatible(int,int,int,int)}. </li>
|
54 |
|
|
* <li> If scanUnconfigured is true, also devices without ZTEX Firmware and devices with Cypress EZ-USB USB are considered</li>
|
55 |
|
|
* <li> If more than one device is present a single device can be selected by serial number. </li>
|
56 |
|
|
* </ol>
|
57 |
|
|
* {@link #unref()} must be called if the class is not used anymore.
|
58 |
|
|
* @param usbVendorId USB vendor ID of the device to be searched for
|
59 |
|
|
* @param usbProductId USB product ID of the device to be searched for
|
60 |
|
|
* @param scanUnconfigured if true, scan for unconfigured devices and devices with Cypress EZ-USB USB ID's
|
61 |
|
|
* @param quiet if true, don't print any warnings
|
62 |
|
|
* @param interfaceVersion The required interface version (<0 if no interface version is required)
|
63 |
|
|
* @param snString The serial number of the device
|
64 |
|
|
* @param productId0 Byte 0 of a given ZTEX product ID (≤0 if not to be considered)
|
65 |
|
|
* @param productId1 Byte 1 of a given ZTEX product ID (≤0 if not to be considered)
|
66 |
|
|
* @param productId2 Byte 2 of a given ZTEX product ID (≤0 if not to be considered)
|
67 |
|
|
* @param productId3 Byte 3 of a given ZTEX product ID (≤0 if not to be considered)
|
68 |
|
|
*/
|
69 |
|
|
public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanUnconfigured, boolean quiet, int interfaceVersion, String snString, int productId0, int productId1, int productId2, int productId3 ) {
|
70 |
|
|
// create a new context for every scan in to ensure an up to date device list
|
71 |
|
|
ZtexContext context;
|
72 |
|
|
try {
|
73 |
|
|
context = new ZtexContext();
|
74 |
|
|
}
|
75 |
|
|
catch ( UsbException e ) {
|
76 |
|
|
if ( ! quiet ) System.err.println( e.getLocalizedMessage() );
|
77 |
|
|
return;
|
78 |
|
|
}
|
79 |
|
|
refCount = 1;
|
80 |
|
|
|
81 |
|
|
// Read the USB device list
|
82 |
|
|
DeviceList dl = new DeviceList();
|
83 |
|
|
int result = LibUsb.getDeviceList(context.context(), dl);
|
84 |
|
|
if (result < 0) {
|
85 |
|
|
context.unref();
|
86 |
|
|
if ( ! quiet ) System.err.println( "Unable to get device list: " + LibUsb.strError(result));
|
87 |
|
|
return;
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
// Iterate over all devices
|
91 |
|
|
for (Device dev: dl) {
|
92 |
|
|
try {
|
93 |
|
|
ZtexDevice1 zdev = new ZtexDevice1(context, dev, usbVendorId, usbProductId, scanUnconfigured );
|
94 |
|
|
if ( scanUnconfigured ||
|
95 |
|
|
( zdev.valid() &&
|
96 |
|
|
( interfaceVersion<0 || zdev.interfaceVersion()==interfaceVersion ) &&
|
97 |
|
|
( snString == null || zdev.snString().equals(snString) ) &&
|
98 |
|
|
zdev.compatible(productId0, productId1, productId2, productId3)
|
99 |
|
|
) ) devices.add( zdev );
|
100 |
|
|
}
|
101 |
|
|
catch ( DeviceNotSupportedException e ) {
|
102 |
|
|
}
|
103 |
|
|
catch ( Exception e ) {
|
104 |
|
|
if ( ! quiet ) System.err.println( e.getLocalizedMessage() );
|
105 |
|
|
}
|
106 |
|
|
}
|
107 |
|
|
// release resources
|
108 |
|
|
LibUsb.freeDeviceList(dl, true);
|
109 |
|
|
context.unref();
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
/**
|
113 |
|
|
* Scans the USB for suitable devices and constructs a list of them.
|
114 |
|
|
* Three kinds of search filters can be applied
|
115 |
|
|
* <ol>
|
116 |
|
|
* <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>
|
117 |
|
|
* <li> If a certain interface version is required, it can be specified using interfaceVersion. </li>
|
118 |
|
|
* <li> If scanUnconfigured is true, also devices without ZTEX Firmware and devices with Cypress EZ-USB USB are considered</li>
|
119 |
|
|
* <li> In multi device environment a single device can be selected by giving a serial number. </li>
|
120 |
|
|
* </ol>
|
121 |
|
|
* @param usbVendorId USB vendor ID of the device to be searched for
|
122 |
|
|
* @param usbProductId USB product ID of the device to be searched for
|
123 |
|
|
* @param scanUnconfigured if true, scan for unconfigured devices and devices with Cypress EZ-USB USB ID's
|
124 |
|
|
* @param quiet if true, don't print any warnings
|
125 |
|
|
* @param interfaceVersion The required interface version (<0 if no interface version is required)
|
126 |
|
|
* @param snString The serial number of the device
|
127 |
|
|
*/
|
128 |
|
|
public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanUnconfigured, boolean quiet, int interfaceVersion, String snString ) {
|
129 |
|
|
this(usbVendorId, usbProductId, scanUnconfigured, quiet, interfaceVersion, snString, -1,-1,-1,-1 );
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
/**
|
133 |
|
|
* Scans the USB for suitable devices and constructs a list of them.
|
134 |
|
|
* Three kinds of search filters can be applied
|
135 |
|
|
* <ol>
|
136 |
|
|
* <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>
|
137 |
|
|
* <li> If a certain interface version is required, it can be specified using interfaceVersion. </li>
|
138 |
|
|
* <li> If scanUnconfigured is true, also devices without ZTEX Firmware and devices with Cypress EZ-USB USB are considered</li>
|
139 |
|
|
* </ol>
|
140 |
|
|
* @param usbVendorId USB vendor ID of the device to be searched for
|
141 |
|
|
* @param usbProductId USB product ID of the device to be searched for
|
142 |
|
|
* @param scanUnconfigured if true, scan for unconfigured devices and devices with Cypress EZ-USB USB ID's
|
143 |
|
|
* @param quiet if true, don't print any warnings
|
144 |
|
|
* @param interfaceVersion The required interface version (<0 if no interface version is required)
|
145 |
|
|
*/
|
146 |
|
|
public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanUnconfigured, boolean quiet, int interfaceVersion ) {
|
147 |
|
|
this(usbVendorId, usbProductId, scanUnconfigured, quiet, interfaceVersion, null, -1,-1,-1,-1 );
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
/**
|
151 |
|
|
* Scans the USB for suitable devices and constructs a list of them.
|
152 |
|
|
* Two kinds of search filters can be applied
|
153 |
|
|
* <ol>
|
154 |
|
|
* <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>
|
155 |
|
|
* <li> If scanUnconfigured is true, also devices without ZTEX Firmware and devices with Cypress EZ-USB USB are considered</li>
|
156 |
|
|
* </ol>
|
157 |
|
|
* @param usbVendorId USB vendor ID of the device to be searched for
|
158 |
|
|
* @param usbProductId USB product ID of the device to be searched for
|
159 |
|
|
* @param scanUnconfigured if true, scan for unconfigured devices and devices with Cypress EZ-USB USB ID's
|
160 |
|
|
* @param quiet if true, don't print any warnings
|
161 |
|
|
*/
|
162 |
|
|
public ZtexScanBus1 (int usbVendorId, int usbProductId, boolean scanUnconfigured, boolean quiet ) {
|
163 |
|
|
this(usbVendorId, usbProductId, scanUnconfigured, quiet, -1, null, -1,-1,-1,-1 );
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
// ******* printBus ************************************************************
|
167 |
|
|
/**
|
168 |
|
|
* Prints out a list of devices found.
|
169 |
|
|
* @param out Where the output is to be printed to.
|
170 |
|
|
*/
|
171 |
|
|
public void printBus( PrintStream out ) {
|
172 |
|
|
for (int i=0; i<devices.size(); i++ ) {
|
173 |
|
|
out.println( i + ": " + devices.elementAt(i).toString() );
|
174 |
|
|
}
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
// ******* numberOfDevices *****************************************************
|
178 |
|
|
/**
|
179 |
|
|
* Returns the number of devices found.
|
180 |
|
|
* @return the number of devices found.
|
181 |
|
|
*/
|
182 |
|
|
public final int numberOfDevices () {
|
183 |
|
|
return devices.size();
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
// ******* device **************************************************************
|
187 |
|
|
/**
|
188 |
|
|
* Returns a device from the list of devices.
|
189 |
|
|
* @param i The device index.
|
190 |
|
|
* @return a device from the list of devices.
|
191 |
|
|
* @throws IndexOutOfBoundsException if i<0 or i≥{@link #numberOfDevices()}
|
192 |
|
|
*/
|
193 |
|
|
public final ZtexDevice1 device (int i) throws IndexOutOfBoundsException {
|
194 |
|
|
if ( i<0 || i>=devices.size() )
|
195 |
|
|
throw new IndexOutOfBoundsException( "Device number out of range. Valid numbers are 0.." + (devices.size()-1) );
|
196 |
|
|
return devices.elementAt(i);
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
// ******* ref *****************************************************************
|
200 |
|
|
/**
|
201 |
|
|
* Increases reference counter.
|
202 |
|
|
*/
|
203 |
|
|
public synchronized void ref() {
|
204 |
|
|
refCount ++;
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
// ******* unref ***************************************************************
|
208 |
|
|
/**
|
209 |
|
|
* Decreases reference counter and releases resources if 0 is reached.
|
210 |
|
|
*/
|
211 |
|
|
public synchronized void unref() {
|
212 |
|
|
refCount --;
|
213 |
|
|
if ( refCount == 0 ) {
|
214 |
|
|
for (int i=0; i<devices.size(); i++ ) {
|
215 |
|
|
devices.elementAt(i).unref();
|
216 |
|
|
}
|
217 |
|
|
}
|
218 |
|
|
}
|
219 |
|
|
|
220 |
|
|
// ******* finalize ************************************************************
|
221 |
|
|
/**
|
222 |
|
|
* Releases all resources.
|
223 |
|
|
*/
|
224 |
|
|
protected void finalize() throws Throwable {
|
225 |
|
|
if (refCount > 0) refCount=1;
|
226 |
|
|
unref();
|
227 |
|
|
super.finalize();
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
}
|