-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmain.go
71 lines (58 loc) · 2.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) 2015-2025 The libusb developers. All rights reserved.
// Project site: https://github.com/gotmc/libusb
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.
package main
import (
"fmt"
"log"
"github.com/gotmc/libusb/v2"
)
func main() {
// Create a new libusb context
ctx, err := libusb.NewContext()
if err != nil {
log.Fatalf("Error creating context: %s", err)
}
defer ctx.Close()
// Get list of devices
devices, err := ctx.DeviceList()
if err != nil {
log.Fatalf("Error getting device list: %s", err)
}
defer devices.Free()
fmt.Printf("Found %d USB devices\n", len(devices))
// Loop through all devices
for i, device := range devices {
desc, err := device.DeviceDescriptor()
if err != nil {
log.Printf("Error getting device descriptor for device %d: %s", i, err)
continue
}
// Print the device class information
fmt.Printf("\nDevice %d: VID=0x%04x, PID=0x%04x\n", i, desc.VendorID, desc.ProductID)
fmt.Printf(" Device Class: %s (0x%02x)\n", desc.DeviceClass, byte(desc.DeviceClass))
// If this is a per-interface class device, inspect the interfaces
if desc.DeviceClass == libusb.perInterface {
fmt.Println(" This device uses per-interface class codes. Checking interfaces...")
// Find printer interfaces (class 7)
printerIfaces, err := device.FindInterfacesByClass(libusb.InterfaceClassPrinter)
if err != nil {
log.Printf(" Error getting interface info: %s", err)
continue
}
if len(printerIfaces) > 0 {
fmt.Printf(" Found %d printer interface(s)!\n", len(printerIfaces))
// Print details about each printer interface
for j, iface := range printerIfaces {
fmt.Printf(" Printer Interface %d:\n", j)
fmt.Printf(" Interface Number: %d\n", iface.InterfaceNumber)
fmt.Printf(" Interface Class: 0x%02x (Printer)\n", iface.InterfaceClass)
fmt.Printf(" Interface SubClass: 0x%02x\n", iface.InterfaceSubClass)
fmt.Printf(" Interface Protocol: 0x%02x\n", iface.InterfaceProtocol)
fmt.Printf(" Endpoints: %d\n", iface.NumEndpoints)
}
}
}
}
}