⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 showtree.java

📁 JAVA 访问USB JAVA 访问USB JAVA 访问USB JAVA 访问USB JAVA 访问USB JAVA 访问USB
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Java USB Library
 * Copyright (C) 2000 by David Brownell
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package usb.util;

import java.io.IOException;
import java.util.Date;
import java.util.Locale;
import java.util.Vector;

import usb.core.*;

// FIXME -- we need a generic driver framework, perhaps
// driven by XML config files to give binding info.

import usb.devices.Rio500;
import usb.devices.Kodak;


/**
 * Displays the host's Universal Serial Bus (USB) in XML on standard output.
 * This scans the preliminary USB device filesystem and looks at each
 * device; it needs read/write access to a device file in order to do
 * anything even vaguely intereseting with the device, but one hopes
 * that will change over time.
 *
 * <p> This is here as a debug-and-get-going facility.  It expects to
 * be able to read (and, sadly, write) all /proc/bus/usb/BBB/DDD nodes;
 * unfortunately, all these reads currently count as writes due to the
 * odd permissions model currently used by usbdevfs.
 *
 * @version $Id: ShowTree.java,v 1.20 2001/01/02 21:11:26 dbrownell Exp $
 */
final public class ShowTree
{
    static int		defaultLanguage;

    private ShowTree () {}

    public static void main (String argv [])
    {
	Bus		busses [] = null;
	Host		host;

	try {

	    // FIXME:  option flag for rmi (iiop?) host:port spec.

	    host = HostFactory.getHost ();
	    if (host == null) {
		System.err.println ("USB is unavailable, can't run.");
		return;
	    }

	    System.out.println ("<!-- " + host + " -->");

	    busses = host.getBusses ();
	    indentLine (0, "<host busses='" + busses.length + "'>");
	    for (int busno = 0; busno < busses.length; busno++) {
		indentLine (2, "<!-- Bus #" + (busno + 1) + " -->");
		// show tree from root hub
		if (busses [busno] != null) {
		    try { printDevice (2, busses [busno].getRootHub ()); }
		    catch (IOException e) { e.printStackTrace (); }
		}
	    }
	    indentLine (0, "</host>");

	} catch (SecurityException e) {
	    System.err.println ("USB permissions problem:");
	    System.err.println (e.getMessage ());
	    System.exit (1);

	} catch (Exception e) {
	    e.printStackTrace ();
	}
    }

    static void indentLine (int n, String s)
    {
	for (; n >= 8; n -= 8)
	    System.out.print ('\t');
	while (n-- > 0)
	    System.out.print (' ');
	System.out.println (s);
    }

    private static void printDevice (int indent, Device dev)
    throws IOException
    {
	String	temp;
	int	langs [];

	// typical case:  most device slots are empty
	if (dev == null)
	    return;

	langs = ControlMessage.getLanguages (dev);
	if (langs != null && langs.length > 0)
	    defaultLanguage = langs [0];
	else
	    defaultLanguage = 0;
	
	//
	// two kinds of address:  port ID, bus device number
	//
	indentLine (indent, "<?addresses portid='"
	    + dev.getPortIdentifier ()
	    + "' busaddr='"
	    + dev.getAddress ()
	    + "'?>");

	//
	// device descriptor
	//
	DeviceDescriptor	info = dev.getDeviceDescriptor ();

	indentLine (indent, "<"
	    + info.getDeviceClassName ()
	    + " usb='"
	    + info.getUSBVersion ()
	    + "'");
	
	indentLine (indent + 4, "vendorId='0x"
	    + Integer.toHexString (info.getVendorId ())
	    + "' productId='0x"
	    + Integer.toHexString (info.getProductId ())
	    + "' device-version='"
	    + info.getDeviceId ()
	    + "'");
	
	temp = info.getManufacturer (defaultLanguage);
	if (temp != null)
	    indentLine (indent +4, "manufacturer='" + temp + "'");
	temp = info.getProduct (defaultLanguage);
	if (temp != null)
	    indentLine (indent + 4, "product='" + temp + "'");
	temp = info.getSerial (defaultLanguage);
	if (temp != null)
	    indentLine (indent + 4, "serial='" + temp + "'");
	
	indentLine (indent + 4, "class='"
	    + info.getDeviceClass ()
	    + "' subclass='"
	    + info.getDeviceSubClass ()
	    + "' protocol='"
	    + info.getDeviceProtocol ()
	    + "'");
	indentLine (indent + 4, "ep0-maxpacket='"
	    + Integer.toString (info.getMaxPacketSize0 ())
	    + "' configurations='"
	    + info.getNumConfigurations ()
	    + "'>");
	indent += 2;

	if (langs != null) {
	    for (int x = 0; x < langs.length; x++) {
		Locale	locale = LangCode.getLocale (langs [x]);
		String	tmp = "";

		if (locale != null)
		    tmp = "' locale='" + locale.toString ();
		indentLine (indent, "<language id='0x"
		    + Integer.toHexString (langs [x])
		    + tmp
		    + "'/>");
	    }
	}
	
	for (int i = 0; i < info.getNumConfigurations (); i++)
	    try {
		printConfiguration (indent, dev.getConfiguration (i));
	    } catch (IOException e) {
		indentLine (indent, "<!-- CAN'T GET CONFIGURATION: ");
		e.printStackTrace (System.out);
		indentLine (indent, "-->");
	    }
	maybePrintDeviceDetails (indent, info);
	indentLine (indent, "</" + info.getDeviceClassName () + ">");
    }

    private static void printConfiguration (int indent, Configuration c)
    {
	String	temp;

	// atypical:  should always be able to read configuration
	if (c == null) {
	    indentLine (indent, "<!-- null configuration -->");
	    return;
	}

	indentLine (indent, "<config value='"
	    + c.getConfigurationValue ()
	    + "' total-length='"
	    + c.getTotalLength ()
	    + "' attributes='"
	    + Integer.toHexString (c.getAttributes ())
	    + "'");
	temp = c.getConfiguration (defaultLanguage);
	if (temp != null)
	    indentLine (indent + 4, "description='"
		+ temp
		+ "'");
	indentLine (indent + 4, "max-power='"
	    + (2 * c.getMaxPower ())
	    + " mA.' interfaces='"
	    + c.getNumInterfaces ()
	    + "'>");
	indent += 2;

	maybePrintDescriptors (indent, c.nextDescriptor ());
	
	for (int i = 0; i < c.getNumInterfaces (); i++) {
	    try {
		Interface	intf;

		// NOTE:  assumes altsetting range is contiguous, [0..N] 
		for (int j = 0;
			(intf = c.getInterface (i, j)) != null;
			j++) {
		    // claimer of interface selects altsettings
		    if (j == 0) {
			String claimer = intf.getClaimer ();
			if (claimer != null)
			    indentLine (indent, "<!-- interface "
				+ i
				+ " is claimed by: "
				+ claimer
				+ " driver -->");
			else
			    indentLine (indent, "<!-- interface "
				+ i
				+ " is unclaimed -->");
		    }
		    printInterface (indent, intf);
		}
	    } catch (IOException e) {
		indentLine (indent, "<!-- CAN'T GET INTERFACE: ");
		e.printStackTrace (System.out);
		indentLine (indent, "-->");
	    }
	}
	indentLine (indent, "</config>");
    }

    private static void printInterface (int indent, Interface intf)
    throws IOException
    {
	indentLine (indent, "<"
	    + intf.getInterfaceClassName ()
	    + " number='"
	    + intf.getNumber ()
	    + "' alt='"
	    + intf.getAlternateSetting ()
	    + "' endpoints='"
	    + intf.getNumEndpoints ()
	    + "'");
	indentLine (indent + 4, "class='"
	    + intf.getInterfaceClass ()
	    + "' subclass='"
	    + intf.getInterfaceSubClass ()
	    + "' protocol='"
	    + intf.getInterfaceProtocol ()
	    + "'>");
	indent += 2;

	maybePrintDescriptors (indent, intf.nextDescriptor ());
	
	for (int ep = 0; ep < intf.getNumEndpoints (); ep++)
	    try {
		printEndpoint (indent, intf.getEndpoint (ep));
	    } catch (IOException e) {
		indentLine (indent, "<!-- CAN'T GET ENDPOINT: ");
		e.printStackTrace (System.out);
		indentLine (indent, "-->");
	    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -