Parcourir la source

Работа с библиотекой javax-usb

Илья Егоров il y a 8 ans
Parent
commit
ae6a906217

+ 6 - 2
nbproject/project.properties

@@ -32,8 +32,10 @@ file.reference.commons-lang3-3.2.1.jar-1=C:\\Users\\User\\Desktop\\libusb\\commo
 file.reference.libusb4java-1.2.0-windows-x86.jar=C:\\Users\\ilysk\\Desktop\\Work&Learn\\Coding\\Java\\Libraries\\usb4java-1.2.0\\lib\\libusb4java-1.2.0-windows-x86.jar
 file.reference.libusb4java-1.2.0-windows-x86.jar-1=C:\\Users\\User\\Desktop\\libusb\\libusb4java-1.2.0-windows-x86.jar
 file.reference.libusb4java-1.2.0-windows-x86_64.jar-1=C:\\Users\\User\\Desktop\\libusb\\libusb4java-1.2.0-windows-x86_64.jar
+file.reference.usb-api-1.0.2.jar=C:\\Users\\User\\Desktop\\libusb\\usb-api-1.0.2.jar
 file.reference.usb4java-1.2.0.jar=C:\\Users\\ilysk\\Desktop\\Work&Learn\\Coding\\Java\\Libraries\\usb4java-1.2.0\\lib\\usb4java-1.2.0.jar
 file.reference.usb4java-1.2.0.jar-1=C:\\Users\\User\\Desktop\\libusb\\usb4java-1.2.0.jar
+file.reference.usb4java-javax-1.2.0.jar=C:\\Users\\User\\Desktop\\libusb\\usb4java-javax-1.2.0.jar
 includes=**
 jar.compress=false
 javac.classpath=\
@@ -43,7 +45,9 @@ javac.classpath=\
     ${file.reference.commons-lang3-3.2.1.jar-1}:\
     ${file.reference.libusb4java-1.2.0-windows-x86.jar-1}:\
     ${file.reference.libusb4java-1.2.0-windows-x86_64.jar-1}:\
-    ${file.reference.usb4java-1.2.0.jar-1}
+    ${file.reference.usb4java-1.2.0.jar-1}:\
+    ${file.reference.usb-api-1.0.2.jar}:\
+    ${file.reference.usb4java-javax-1.2.0.jar}
 # Space-separated list of extra javac options
 javac.compilerargs=
 javac.deprecation=false
@@ -67,7 +71,7 @@ javadoc.splitindex=true
 javadoc.use=true
 javadoc.version=false
 javadoc.windowtitle=
-main.class=project82usb.devicesmatrix
+main.class=project82usb.devMatrix
 manifest.file=manifest.mf
 meta.inf.dir=${src.dir}/META-INF
 mkdist.disabled=false

+ 1 - 0
src/javax.usb.properties

@@ -0,0 +1 @@
+javax.usb.services = org.usb4java.javax.Services

+ 16 - 5
src/project82usb/devicesmatrix.form

@@ -29,7 +29,9 @@
               <Group type="103" groupAlignment="0" attributes="0">
                   <Group type="102" alignment="1" attributes="0">
                       <EmptySpace min="0" pref="0" max="32767" attributes="0"/>
-                      <Component id="btnRefresh" min="-2" max="-2" attributes="0"/>
+                      <Component id="btnRefreshHigh" min="-2" max="-2" attributes="0"/>
+                      <EmptySpace type="separate" max="-2" attributes="0"/>
+                      <Component id="btnRefreshLow" min="-2" max="-2" attributes="0"/>
                       <EmptySpace type="separate" max="-2" attributes="0"/>
                       <Component id="btnClear" min="-2" max="-2" attributes="0"/>
                   </Group>
@@ -48,7 +50,8 @@
               <EmptySpace type="unrelated" max="-2" attributes="0"/>
               <Group type="103" groupAlignment="3" attributes="0">
                   <Component id="btnClear" alignment="3" min="-2" max="-2" attributes="0"/>
-                  <Component id="btnRefresh" alignment="3" min="-2" max="-2" attributes="0"/>
+                  <Component id="btnRefreshLow" alignment="3" min="-2" max="-2" attributes="0"/>
+                  <Component id="btnRefreshHigh" alignment="3" min="-2" max="-2" attributes="0"/>
               </Group>
               <EmptySpace max="-2" attributes="0"/>
           </Group>
@@ -101,12 +104,20 @@
         <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnClearActionPerformed"/>
       </Events>
     </Component>
-    <Component class="javax.swing.JButton" name="btnRefresh">
+    <Component class="javax.swing.JButton" name="btnRefreshLow">
+      <Properties>
+        <Property name="text" type="java.lang.String" value="Low API"/>
+      </Properties>
+      <Events>
+        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnRefreshLowActionPerformed"/>
+      </Events>
+    </Component>
+    <Component class="javax.swing.JButton" name="btnRefreshHigh">
       <Properties>
-        <Property name="text" type="java.lang.String" value="Refresh"/>
+        <Property name="text" type="java.lang.String" value="High API"/>
       </Properties>
       <Events>
-        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnRefreshActionPerformed"/>
+        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnRefreshHighActionPerformed"/>
       </Events>
     </Component>
   </SubComponents>

+ 34 - 16
src/project82usb/devicesmatrix.java

@@ -10,12 +10,13 @@ import project82usb.usbDeviceList.*;
  *
  * @author ilysk
  */
-public class devicesmatrix extends javax.swing.JFrame {
+public class devMatrix extends javax.swing.JFrame {
     private final usbDeviceList list = new usbDeviceList();
+    private final usbDeviceTree tree = new usbDeviceTree();
     /**
      * Creates new form devicesmatrix
      */
-    public devicesmatrix() {
+    public devMatrix() {
         initComponents();
     }
 
@@ -33,7 +34,8 @@ public class devicesmatrix extends javax.swing.JFrame {
         jScrollPane2 = new javax.swing.JScrollPane();
         tvOut = new javax.swing.JTextArea();
         btnClear = new javax.swing.JButton();
-        btnRefresh = new javax.swing.JButton();
+        btnRefreshLow = new javax.swing.JButton();
+        btnRefreshHigh = new javax.swing.JButton();
 
         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
@@ -62,10 +64,17 @@ public class devicesmatrix extends javax.swing.JFrame {
             }
         });
 
-        btnRefresh.setText("Refresh");
-        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
+        btnRefreshLow.setText("Low API");
+        btnRefreshLow.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
-                btnRefreshActionPerformed(evt);
+                btnRefreshLowActionPerformed(evt);
+            }
+        });
+
+        btnRefreshHigh.setText("High API");
+        btnRefreshHigh.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                btnRefreshHighActionPerformed(evt);
             }
         });
 
@@ -79,7 +88,9 @@ public class devicesmatrix extends javax.swing.JFrame {
                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                         .addGap(0, 0, Short.MAX_VALUE)
-                        .addComponent(btnRefresh)
+                        .addComponent(btnRefreshHigh)
+                        .addGap(18, 18, 18)
+                        .addComponent(btnRefreshLow)
                         .addGap(18, 18, 18)
                         .addComponent(btnClear))
                     .addComponent(jScrollPane2))
@@ -94,7 +105,8 @@ public class devicesmatrix extends javax.swing.JFrame {
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                     .addComponent(btnClear)
-                    .addComponent(btnRefresh))
+                    .addComponent(btnRefreshLow)
+                    .addComponent(btnRefreshHigh))
                 .addContainerGap())
         );
 
@@ -105,10 +117,14 @@ public class devicesmatrix extends javax.swing.JFrame {
         this.tvOut.setText("");
     }//GEN-LAST:event_btnClearActionPerformed
 
-    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
+    private void btnRefreshLowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshLowActionPerformed
         list.getUsbDeviceList();
         list.printDeviceList(this);
-    }//GEN-LAST:event_btnRefreshActionPerformed
+    }//GEN-LAST:event_btnRefreshLowActionPerformed
+
+    private void btnRefreshHighActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshHighActionPerformed
+        tree.print(this);
+    }//GEN-LAST:event_btnRefreshHighActionPerformed
 
     /**
      * @param args the command line arguments
@@ -127,20 +143,21 @@ public class devicesmatrix extends javax.swing.JFrame {
                 }
             }
         } catch (ClassNotFoundException ex) {
-            java.util.logging.Logger.getLogger(devicesmatrix.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+            java.util.logging.Logger.getLogger(devMatrix.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
         } catch (InstantiationException ex) {
-            java.util.logging.Logger.getLogger(devicesmatrix.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+            java.util.logging.Logger.getLogger(devMatrix.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
         } catch (IllegalAccessException ex) {
-            java.util.logging.Logger.getLogger(devicesmatrix.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+            java.util.logging.Logger.getLogger(devMatrix.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
-            java.util.logging.Logger.getLogger(devicesmatrix.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+            java.util.logging.Logger.getLogger(devMatrix.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
         }
         //</editor-fold>
+        //</editor-fold>
 
         /* Create and display the form */
         java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
-                new devicesmatrix().setVisible(true);
+                new devMatrix().setVisible(true);
             }
         });
     }
@@ -151,7 +168,8 @@ public class devicesmatrix extends javax.swing.JFrame {
 
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JButton btnClear;
-    private javax.swing.JButton btnRefresh;
+    private javax.swing.JButton btnRefreshHigh;
+    private javax.swing.JButton btnRefreshLow;
     private javax.swing.JScrollPane jScrollPane1;
     private javax.swing.JScrollPane jScrollPane2;
     private javax.swing.JTable jTable1;

+ 2 - 2
src/project82usb/usbDeviceList.java

@@ -12,7 +12,7 @@ import org.usb4java.DeviceList;
 import org.usb4java.LibUsb;
 import org.usb4java.LibUsbException;
 import java.util.ArrayList;
-import project82usb.devicesmatrix.*;
+import project82usb.devMatrix.*;
 
 /**
  *
@@ -47,7 +47,7 @@ public class usbDeviceList {
             LibUsb.freeDeviceList(list, true);
         }       
     }
-    public void printDeviceList(devicesmatrix out){
+    public void printDeviceList(devMatrix out){
         out.addTextTvOut("Printing device list:");
         for(usbDevice device:this.devList){
             out.addTextTvOut("Device "+device.getStringIdVendor()+":"+device.getStringIdProduct()+" on port  #"+device.getPort());

+ 72 - 0
src/project82usb/usbDeviceTree.java

@@ -0,0 +1,72 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package project82usb;
+import java.io.UnsupportedEncodingException;
+import javax.usb.*;
+import java.util.*;
+/**
+ *
+ * @author User
+ */
+public class usbDeviceTree {
+    private static final String PREFIX = "   ";
+    private final UsbHub rootHub;
+    
+    public usbDeviceTree() {
+        this.rootHub = VirtualRootUsbHub();
+    }
+    
+    private UsbHub VirtualRootUsbHub(){
+        UsbServices services = null;
+        UsbHub VirtualRootUsbHub = null;
+        
+        try{
+            services = UsbHostManager.getUsbServices();
+        } catch (UsbException | SecurityException e){
+            throw new RuntimeException("Error : " + e.getMessage());
+        }
+        
+        try {
+            VirtualRootUsbHub = services.getRootUsbHub();
+        } catch (UsbException | SecurityException e){
+            throw new RuntimeException("Error : " + e.getMessage());
+        }
+        
+        return VirtualRootUsbHub;
+    }
+    
+    public UsbHub getVirtualRootUsbHub(){
+        return this.rootHub;
+    }
+    
+    public void print(devMatrix out){
+        processPrintUsbPorts(rootHub,"",out);
+        out.addTextTvOut("");
+    }
+    
+    private void processPrintUsbPorts(UsbDevice device, String prefix, devMatrix out){
+        UsbHub hub = null;
+        //Проверяем, является ли device хабом
+        if(!device.isUsbHub()){
+            out.addTextTvOut(prefix+device.toString()+" on port #"+device.getParentUsbPort().getPortNumber());
+            return;
+        } else{
+            hub = (UsbHub)device;
+        }
+        out.addTextTvOut(prefix+"UsbHub: "+hub.toString().substring(9));
+        //Получаем список портов
+        List usbPorts = hub.getUsbPorts();
+        for(int i=0;i<usbPorts.size();i++){
+           UsbPort port = (UsbPort) usbPorts.get(i);
+           //Проверяем, подключены ли к порту устройства
+           if(!port.isUsbDeviceAttached()){
+               out.addTextTvOut(prefix+PREFIX+"usbPort "+port.toString());
+           } else{
+               processPrintUsbPorts(port.getUsbDevice(),prefix+PREFIX,out);
+           }
+        }
+    }
+}