2 Commits 1e2452b6c9 ... b22359d44d

Autor SHA1 Nachricht Datum
  Илья Егоров b22359d44d Merge origin/Egorov into Egorov vor 8 Jahren
  Илья Егоров ae6a906217 Работа с библиотекой javax-usb vor 8 Jahren

+ 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

+ 0 - 113
src/project82usb/devicesmatrix.form

@@ -1,113 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" ?>
-
-<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
-  <Properties>
-    <Property name="defaultCloseOperation" type="int" value="3"/>
-  </Properties>
-  <SyntheticProperties>
-    <SyntheticProperty name="formSizePolicy" type="int" value="1"/>
-    <SyntheticProperty name="generateCenter" type="boolean" value="false"/>
-  </SyntheticProperties>
-  <AuxValues>
-    <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
-    <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
-    <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
-    <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
-    <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
-    <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
-    <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
-    <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
-    <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
-  </AuxValues>
-
-  <Layout>
-    <DimensionLayout dim="0">
-      <Group type="103" groupAlignment="0" attributes="0">
-          <Component id="jScrollPane1" pref="400" max="32767" attributes="0"/>
-          <Group type="102" attributes="0">
-              <EmptySpace max="-2" attributes="0"/>
-              <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"/>
-                      <EmptySpace type="separate" max="-2" attributes="0"/>
-                      <Component id="btnClear" min="-2" max="-2" attributes="0"/>
-                  </Group>
-                  <Component id="jScrollPane2" max="32767" attributes="0"/>
-              </Group>
-              <EmptySpace max="-2" attributes="0"/>
-          </Group>
-      </Group>
-    </DimensionLayout>
-    <DimensionLayout dim="1">
-      <Group type="103" groupAlignment="0" attributes="0">
-          <Group type="102" alignment="0" attributes="0">
-              <Component id="jScrollPane1" min="-2" pref="126" max="-2" attributes="0"/>
-              <EmptySpace max="-2" attributes="0"/>
-              <Component id="jScrollPane2" pref="254" max="32767" attributes="0"/>
-              <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"/>
-              </Group>
-              <EmptySpace max="-2" attributes="0"/>
-          </Group>
-      </Group>
-    </DimensionLayout>
-  </Layout>
-  <SubComponents>
-    <Container class="javax.swing.JScrollPane" name="jScrollPane1">
-      <AuxValues>
-        <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
-      </AuxValues>
-
-      <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
-      <SubComponents>
-        <Component class="javax.swing.JTable" name="jTable1">
-          <Properties>
-            <Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor">
-              <Table columnCount="4" rowCount="4">
-                <Column editable="true" title="Title 1" type="java.lang.Object"/>
-                <Column editable="true" title="Title 2" type="java.lang.Object"/>
-                <Column editable="true" title="Title 3" type="java.lang.Object"/>
-                <Column editable="true" title="Title 4" type="java.lang.Object"/>
-              </Table>
-            </Property>
-          </Properties>
-        </Component>
-      </SubComponents>
-    </Container>
-    <Container class="javax.swing.JScrollPane" name="jScrollPane2">
-      <AuxValues>
-        <AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
-      </AuxValues>
-
-      <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
-      <SubComponents>
-        <Component class="javax.swing.JTextArea" name="tvOut">
-          <Properties>
-            <Property name="editable" type="boolean" value="false"/>
-            <Property name="columns" type="int" value="20"/>
-            <Property name="rows" type="int" value="5"/>
-          </Properties>
-        </Component>
-      </SubComponents>
-    </Container>
-    <Component class="javax.swing.JButton" name="btnClear">
-      <Properties>
-        <Property name="text" type="java.lang.String" value="Clear"/>
-      </Properties>
-      <Events>
-        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnClearActionPerformed"/>
-      </Events>
-    </Component>
-    <Component class="javax.swing.JButton" name="btnRefresh">
-      <Properties>
-        <Property name="text" type="java.lang.String" value="Refresh"/>
-      </Properties>
-      <Events>
-        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnRefreshActionPerformed"/>
-      </Events>
-    </Component>
-  </SubComponents>
-</Form>

+ 0 - 160
src/project82usb/devicesmatrix.java

@@ -1,160 +0,0 @@
-/*
- * 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 project82usb.usbDeviceList.*;
-
-/**
- *
- * @author ilysk
- */
-public class devicesmatrix extends javax.swing.JFrame {
-    private final usbDeviceList list = new usbDeviceList();
-    /**
-     * Creates new form devicesmatrix
-     */
-    public devicesmatrix() {
-        initComponents();
-    }
-
-    /**
-     * This method is called from within the constructor to initialize the form.
-     * WARNING: Do NOT modify this code. The content of this method is always
-     * regenerated by the Form Editor.
-     */
-    @SuppressWarnings("unchecked")
-    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
-    private void initComponents() {
-
-        jScrollPane1 = new javax.swing.JScrollPane();
-        jTable1 = new javax.swing.JTable();
-        jScrollPane2 = new javax.swing.JScrollPane();
-        tvOut = new javax.swing.JTextArea();
-        btnClear = new javax.swing.JButton();
-        btnRefresh = new javax.swing.JButton();
-
-        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
-
-        jTable1.setModel(new javax.swing.table.DefaultTableModel(
-            new Object [][] {
-                {null, null, null, null},
-                {null, null, null, null},
-                {null, null, null, null},
-                {null, null, null, null}
-            },
-            new String [] {
-                "Title 1", "Title 2", "Title 3", "Title 4"
-            }
-        ));
-        jScrollPane1.setViewportView(jTable1);
-
-        tvOut.setEditable(false);
-        tvOut.setColumns(20);
-        tvOut.setRows(5);
-        jScrollPane2.setViewportView(tvOut);
-
-        btnClear.setText("Clear");
-        btnClear.addActionListener(new java.awt.event.ActionListener() {
-            public void actionPerformed(java.awt.event.ActionEvent evt) {
-                btnClearActionPerformed(evt);
-            }
-        });
-
-        btnRefresh.setText("Refresh");
-        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
-            public void actionPerformed(java.awt.event.ActionEvent evt) {
-                btnRefreshActionPerformed(evt);
-            }
-        });
-
-        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
-        getContentPane().setLayout(layout);
-        layout.setHorizontalGroup(
-            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
-            .addGroup(layout.createSequentialGroup()
-                .addContainerGap()
-                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
-                        .addGap(0, 0, Short.MAX_VALUE)
-                        .addComponent(btnRefresh)
-                        .addGap(18, 18, 18)
-                        .addComponent(btnClear))
-                    .addComponent(jScrollPane2))
-                .addContainerGap())
-        );
-        layout.setVerticalGroup(
-            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addGroup(layout.createSequentialGroup()
-                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
-                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
-                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
-                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
-                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
-                    .addComponent(btnClear)
-                    .addComponent(btnRefresh))
-                .addContainerGap())
-        );
-
-        pack();
-    }// </editor-fold>//GEN-END:initComponents
-
-    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
-        this.tvOut.setText("");
-    }//GEN-LAST:event_btnClearActionPerformed
-
-    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
-        list.getUsbDeviceList();
-        list.printDeviceList(this);
-    }//GEN-LAST:event_btnRefreshActionPerformed
-
-    /**
-     * @param args the command line arguments
-     */
-    public static void main(String args[]) {
-        /* Set the Nimbus look and feel */
-        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
-        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
-         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
-         */
-        try {
-            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
-                if ("Nimbus".equals(info.getName())) {
-                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
-                    break;
-                }
-            }
-        } catch (ClassNotFoundException ex) {
-            java.util.logging.Logger.getLogger(devicesmatrix.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);
-        } catch (IllegalAccessException ex) {
-            java.util.logging.Logger.getLogger(devicesmatrix.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);
-        }
-        //</editor-fold>
-
-        /* Create and display the form */
-        java.awt.EventQueue.invokeLater(new Runnable() {
-            public void run() {
-                new devicesmatrix().setVisible(true);
-            }
-        });
-    }
-    
-    public void addTextTvOut(String text){
-        tvOut.setText(tvOut.getText()+text+"\n");
-    }
-
-    // Variables declaration - do not modify//GEN-BEGIN:variables
-    private javax.swing.JButton btnClear;
-    private javax.swing.JButton btnRefresh;
-    private javax.swing.JScrollPane jScrollPane1;
-    private javax.swing.JScrollPane jScrollPane2;
-    private javax.swing.JTable jTable1;
-    private javax.swing.JTextArea tvOut;
-    // End of variables declaration//GEN-END:variables
-}

+ 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);
+           }
+        }
+    }
+}