Documentation
¶
Overview ¶
Package usbhid provides support for interacting with USB HID devices connected to a computer, from userspace.
It is written in pure Go and works on Linux, macOS and Windows.
Index ¶
- Variables
- type Device
- func (d *Device) Close() error
- func (d *Device) GetFeatureReport(reportId byte) ([]byte, error)
- func (d *Device) GetFeatureReportLength() uint16
- func (d *Device) GetInputReport() (byte, []byte, error)
- func (d *Device) GetInputReportLength() uint16
- func (d *Device) GetOutputReportLength() uint16
- func (d *Device) IsOpen() bool
- func (d *Device) Manufacturer() string
- func (d *Device) Open(lock bool) error
- func (d *Device) Path() string
- func (d *Device) Product() string
- func (d *Device) ProductId() uint16
- func (d *Device) SerialNumber() string
- func (d *Device) SetFeatureReport(reportId byte, data []byte) error
- func (d *Device) SetOutputReport(reportId byte, data []byte) error
- func (d *Device) String() string
- func (d *Device) Usage() uint16
- func (d *Device) UsagePage() uint16
- func (d *Device) VendorId() uint16
- func (d *Device) Version() uint16
- type DeviceFilterFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrDeviceEnumerationFailed = errors.New("usb hid device enumeration failed") ErrDeviceFailedToClose = errors.New("usb hid device failed to close") ErrDeviceFailedToOpen = errors.New("usb hid device failed to open") ErrDeviceIsClosed = errors.New("usb hid device is closed") ErrDeviceIsOpen = errors.New("usb hid device is open") ErrDeviceLocked = errors.New("usb hid device is locked by another application") ErrGetFeatureReportFailed = errors.New("get usb hid feature report failed") ErrGetInputReportFailed = errors.New("get usb hid input report failed") ErrMoreThanOneDeviceFound = errors.New("more than one usb hid device found") ErrNoDeviceFound = errors.New("no usb hid device found") ErrReportBufferOverflow = errors.New("usb hid report buffer overflow") ErrSetFeatureReportFailed = errors.New("set usb hid feature report failed") ErrSetOutputReportFailed = errors.New("set usb hid output report failed") )
Errors returned from usbhid package may be tested against these errors with errors.Is.
Functions ¶
This section is empty.
Types ¶
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device is an opaque structure that represents a USB HID device connected to the computer.
func Enumerate ¶
func Enumerate(f DeviceFilterFunc) ([]*Device, error)
Enumerate lists the USB HID devices connected to the computer, optionally filtered by a DeviceFilterFunc function.
Example ¶
package main
import (
"fmt"
"log"
"rafaelmartins.com/p/usbhid"
)
func main() {
devices, err := usbhid.Enumerate(nil) // no filtering
if err != nil {
log.Fatal(err)
}
for _, device := range devices {
fmt.Printf("Device: 0x%04x:0x%04x\n", device.VendorId(), device.ProductId())
fmt.Printf("\tManufacturer: %s\n", device.Manufacturer())
fmt.Printf("\tProduct: %s\n", device.Product())
fmt.Printf("\tSerial Number: %s\n", device.SerialNumber())
fmt.Printf("\tUsage: 0x%04x/0x%04x\n", device.UsagePage(), device.Usage())
}
}
func Get ¶
func Get(f DeviceFilterFunc, open bool, lock bool) (*Device, error)
Get returns a USB HID device found connected to the machine that matches the DeviceFilterfunc function. It can optionally open the device and acquire an exclusive lock.
If the filtering would result in more than one device, or zero devices, an error is returned.
func (*Device) GetFeatureReport ¶
GetFeatureReport reads a feature report from the USB HID device. It may block until a report is available, depending on the operating system. It takes the desired report id and returns a slice of bytes with the report content and an error (or nil).
func (*Device) GetFeatureReportLength ¶
GetFeatureReportLength returns the data size of a feature report in bytes.
func (*Device) GetInputReport ¶
GetInputReport reads an input report from the USB HID device. It will block until a report is available, and returns the report id, a slice of bytes with the report content, and an error (or nil).
func (*Device) GetInputReportLength ¶
GetInputReportLength returns the data size of an input report in bytes.
func (*Device) GetOutputReportLength ¶
GetOutputReportLength returns the data size of an output report in bytes.
func (*Device) Manufacturer ¶
Manufacturer returns a string representation of the manufacturer of the USB HID device.
func (*Device) Product ¶
Product returns a string representation of the product name of the USB HID device.
func (*Device) SerialNumber ¶
SerialNumber returns a string representation of the serial number of the USB HID device.
func (*Device) SetFeatureReport ¶
SetFeatureReport writes an output report to the USB HID device. It takes the report id and a slice of bytes with the data to be sent, and returns an error or nil. If the size of the slice is lower than the expected report size, it will be zero padded, and if it is bigger, an error is returned.
func (*Device) SetOutputReport ¶
SetOutputReport writes an output report to the USB HID device. It takes the report id and a slice of bytes with the data to be sent, and returns an error or nil. If the size of the slice is lower than the expected report size, it will be zero padded, and if it is bigger, an error is returned.
type DeviceFilterFunc ¶
DeviceFilterFunc is a function prototype that helps defining a filter function to be used by the device enumeration functions.
Example ¶
package main
import (
"fmt"
"log"
"rafaelmartins.com/p/usbhid"
)
func main() {
device, err := usbhid.Get(func(d *usbhid.Device) bool {
// filtering by free HID VID/PID from v-usb
if d.VendorId() != 0x16c0 {
return false
}
if d.ProductId() != 0x05df {
return false
}
return true
}, false, false)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Device: 0x%04x:0x%04x\n", device.VendorId(), device.ProductId())
fmt.Printf("\tManufacturer: %s\n", device.Manufacturer())
fmt.Printf("\tProduct: %s\n", device.Product())
fmt.Printf("\tSerial Number: %s\n", device.SerialNumber())
}