📄 usbprobe.java
字号:
package net.sf.dz.setup;import java.io.IOException;import java.util.Iterator;import java.util.Set;import java.util.TreeSet;import usb.core.Bus;import usb.core.Configuration;import usb.core.ControlMessage;import usb.core.Descriptor;import usb.core.Device;import usb.core.DeviceDescriptor;import usb.core.Endpoint;import usb.core.Host;import usb.core.HostFactory;import usb.core.Interface;import usb.core.USBException;import org.freehold.jukebox.logger.LogAware;import org.freehold.jukebox.logger.LogChannel;public class UsbProbe extends LogAware { public static final LogChannel CH_UP = new LogChannel("USB Probe"); /** * Walk the USB bus[ses] and find all the devices with a given * <strong>hexadecimal</strong> vendor/product IDs. * * @param deviceDefs A set of descriptors as strings: * "<code>${vendor_id}:${product_id}</code>". * * @return A set of device descriptors for the devices found, as * strings: "<code>${vendor_id}:${product_id}:${serial_number}</code>". */ public Set probe(Set deviceDefs) { Set result = new TreeSet(); try { Host usbHost = HostFactory.getHost(); if ( usbHost == null ) { complain(LOG_WARNING, CH_UP, "No hosts found"); return result; } Bus busSet[] = usbHost.getBusses(); for ( int idx = 0; idx < busSet.length; idx++ ) { Bus bus = busSet[idx]; Device rootHub = bus.getRootHub(); probe(deviceDefs, result, rootHub); } } catch ( IOException ioex ) { complain(LOG_ERR, CH_UP, "Failed to complete the probe:", ioex); } return result; } private void probe(Set deviceDefs, Set result, Device root) throws IOException { if ( root == null ) { return; } DeviceDescriptor dd = root.getDeviceDescriptor(); if ( dd.getDeviceClass() == Descriptor.CLASS_HUB ) { for ( int port = 0; port < root.getNumPorts(); port++ ) { complain(LOG_DEBUG, CH_UP, "Hub " + dd.getSerial(0) + ": port " + (port + 1)); Device child = root.getChild(port + 1); probe(deviceDefs, result, child); } } else { int vendorId = dd.getVendorId(); int productId = dd.getProductId(); String pair = Integer.toHexString(vendorId) + ":" + Integer.toHexString(productId); complain(LOG_DEBUG, CH_UP, "Found: " + pair); if ( deviceDefs.contains(pair) ) { // Aha! Found it! int languageSet[] = ControlMessage.getLanguages(root); int defaultLanguage = 0; if ( languageSet != null && languageSet.length != 0 ) { defaultLanguage = languageSet[0]; } String serial = dd.getSerial(defaultLanguage); result.add(pair + ":" + serial); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -