usbmon

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2025 License: MIT Imports: 3 Imported by: 1

README

Go Reference

USBMon

A lightweight Go wrapper around libudev to simplify monitoring USB device add/remove events. This package abstracts away the complexities of the low-level udev API, providing a simple interface for detecting and responding to USB devices being connected or disconnected.

// monitor USB hotplug events
package main

import (
	"context"
	"fmt"

	"github.com/rubiojr/go-usbmon"
)

func main() {
	// Print device properties when plugged in or unplugged
	filter := &usbmon.ActionFilter{Action: usbmon.ActionAll}
	devs, err := usbmon.ListenFiltered(context.Background(), filter)
	if err != nil {
		panic(err)
	}

	for dev := range devs {
		fmt.Printf("-- Device %s\n", dev.Action())
		fmt.Println("Serial: " + dev.Serial())
		fmt.Println("Path: " + dev.Path())
		fmt.Println("Vendor: " + dev.Vendor())
	}
}

Building

Requirements
  • libudev

Ubuntu/Debian: apt install libudev-dev

Fedora/RHEL: dnf install systemd-devel

Arch Linux: pacman -S systemd-libs

Additional examples

See examples for more usage examples.

License

MIT - See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Listen

func Listen(ctx context.Context) (chan *Device, error)

func ListenFiltered

func ListenFiltered(ctx context.Context, filters ...Filter) (chan *Device, error)

ListenFiltered returns the usb storage devices that match all the filters passed as arguments.

Filters are additive, meaning every device needs to match all the filter arguments.

Example:

Types

type ActionEvent

type ActionEvent string
const (
	ActionAdd    ActionEvent = "add"
	ActionRemove ActionEvent = "remove"
	ActionAll    ActionEvent = "all"
)

type ActionFilter

type ActionFilter struct {
	Action ActionEvent
}

func (*ActionFilter) Matches

func (f *ActionFilter) Matches(dev *Device) bool

type Device

type Device struct {
	// contains filtered or unexported fields
}

func (*Device) Action

func (d *Device) Action() string

func (*Device) Major

func (d *Device) Major() string

func (*Device) Minor

func (d *Device) Minor() string

func (*Device) Path

func (d *Device) Path() string

func (*Device) ProductID added in v1.1.0

func (d *Device) ProductID() string

func (*Device) Properties

func (d *Device) Properties() map[string]string

func (*Device) Serial

func (d *Device) Serial() string

func (*Device) Vendor

func (d *Device) Vendor() string

func (*Device) VendorID added in v1.1.0

func (d *Device) VendorID() string

type Filter

type Filter interface {
	Matches(*Device) bool
}

type Monitor

type Monitor interface {
	DeviceChan(context.Context) (<-chan *udev.Device, error)
}

type PartitionFilter

type PartitionFilter struct {
	Serial string
}

func (*PartitionFilter) Matches

func (f *PartitionFilter) Matches(dev *Device) bool

type SerialFilter

type SerialFilter struct {
	Serial string
}

func (*SerialFilter) Matches

func (f *SerialFilter) Matches(dev *Device) bool

type UdevMonitor

type UdevMonitor struct {
	// contains filtered or unexported fields
}

func NewUdevMonitor

func NewUdevMonitor() *UdevMonitor

func (*UdevMonitor) DeviceChan

func (m *UdevMonitor) DeviceChan(ctx context.Context) (<-chan *udev.Device, <-chan error, error)

Directories

Path Synopsis
Example that filters USB devices by serial number
Example that filters USB devices by serial number

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL