usb

package module
v0.0.0-...-96ba11e Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2022 License: MIT Imports: 18 Imported by: 0

README

usb

Pure Go USB stack

GoDoc Go Report Card MIT license Tag

Logo

usb is a pure Go implementation of the USB stack (no CGo). It is a libusb alternative for situations you need a go-native library.

usb currently only supports linux, but with enough interest or requests, we can add more platforms.

Getting Started

package main

import  "github.com/pzl/usb"

func main() {
    dev, err := usb.VidPid(0x0c45, 0x6300) // get device by IDs
    if err != nil {
        // handle...
    }
}

Usage

The top-level of this project can be used as a high-level library. See the docs at godoc.

The gusb sub-directory can be used as a more low-level library, if that suits your needs. Documentation also at godoc.

Status

Conversion from libusb is underway. Almost all enumeration and device descriptor function is implemented. IO (both sync and async) is in progress.

See API for a direct mapping of libusb calls to usb methods.

Release History

  • 0.0.1
    • Work in Progress

License

MIT

Copyright 2019 Dan Panzarella

See LICENSE file for full License details

Documentation

Overview

Package usb is the high-level interface to working with USB devices in pure Go.

Getting Started

The first task is finding your device. You can optionally set a configuration if the device has multiple, but this is rare. Before communicating with the device, you need to claim an interface. Then you may send to the endpoints in that interface.

Example
package main

import (
	"fmt"

	"github.com/Emposat/usb"
)

func main() {
	dev, err := usb.VidPid(0x0c45, 0x6300) // opens the first device it finds with these
	if err == usb.ErrDeviceNotFound {
		fmt.Println("Not found")
		return
	}
	err = dev.Open()
	if err != nil {
		fmt.Printf("Error opening device: %v\n", err)
		return
	}
	defer dev.Close()

	err = dev.ClaimInterface(1)
	if err != nil {
		fmt.Printf("Error claiming interface: %v\n", err)
		return
	}
	defer dev.ReleaseInterface(1)

	// @todo this is super ugly
	dev.ActiveConfig.Interfaces[1].Endpoints[1].CtrlTransfer( /*...*/ )
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrDeviceNotFound = errors.New("Device not found")
)
View Source
var ErrNotImplemented = errors.New("not implemented")

Functions

This section is empty.

Types

type Configuration

type Configuration struct {
	SelfPowered    bool
	RemoteWakeup   bool
	BatteryPowered bool
	MaxPower       int // in mA
	Value          int
	Interfaces     []Interface
	// contains filtered or unexported fields
}

type Device

type Device struct {
	Bus          int
	Device       int
	Port         int // @todo: keep this up to date with hotplugs, resets?
	Ports        []int
	Vendor       ID
	Product      ID
	Parent       *Device
	Speed        Speed
	Configs      []Configuration
	ActiveConfig *Configuration // can read SYSFSPATH/bConfigurationValue
	// contains filtered or unexported fields
}

func List

func List() ([]*Device, error)
Example
package main

import (
	"fmt"

	"github.com/Emposat/usb"
)

func main() {
	devices, err := usb.List()
	if err != nil {
		//handle
	}

	for _, d := range devices {
		fmt.Printf("%04x:%04x - %s, %s\n", d.Vendor.ID, d.Product.ID, d.Vendor.Name(), d.Product.Name())
	}
}
Output:

func Open

func Open(bus int, dev int) (*Device, error)
Example
package main

import (
	"github.com/Emposat/usb"
)

func main() {
	dev, err := usb.Open(1, 3)
	if err != nil {
		//handle error
	}
	defer dev.Close()

	//do something
}
Output:

func VidPid

func VidPid(vid uint16, pid uint16) (*Device, error)

func (*Device) ClaimInterface

func (d *Device) ClaimInterface(intf int) error

func (*Device) Close

func (d *Device) Close() error

func (*Device) Endpoint

func (d *Device) Endpoint(num int) (*Endpoint, error)

Return endpoint by it's Address number.

func (*Device) GetDriver

func (d *Device) GetDriver(intf int) (string, error)

func (*Device) Interface

func (d *Device) Interface(i int) (*Interface, error)

func (*Device) Open

func (d *Device) Open() error

func (*Device) ReleaseInterface

func (d *Device) ReleaseInterface(intf int) error

func (*Device) Reset

func (d *Device) Reset() error

func (*Device) SetConfiguration

func (d *Device) SetConfiguration(cfg int) error

type Endpoint

type Endpoint struct {
	Address          int
	TransferType     int
	MaxPacketSize    int
	MaxISOPacketSize int
	// contains filtered or unexported fields
}

func (*Endpoint) Bulk

func (e *Endpoint) Bulk()

func (*Endpoint) CtrlTransfer

func (e *Endpoint) CtrlTransfer()

func (*Endpoint) Interrupt

func (e *Endpoint) Interrupt()

type ID

type ID struct {
	ID uint16 // ID number, e.g. 0xF00D
	// contains filtered or unexported fields
}

func (ID) Name

func (i ID) Name() string

type Interface

type Interface struct {
	ID        int // interface number
	Alternate int
	Endpoints []Endpoint
	// contains filtered or unexported fields
}

func (*Interface) Claim

func (i *Interface) Claim() error

Kernel interface release handled automatically

func (*Interface) GetDriver

func (i *Interface) GetDriver() (string, error)

func (*Interface) Release

func (i *Interface) Release() error

Kernel interface re-claim handled automatically

func (*Interface) SetAlt

func (i *Interface) SetAlt() error

type Speed

type Speed int
const (
	SpeedUnknown Speed = iota
	SpeedLow
	SpeedFull
	SpeedHigh
	SpeedWireless
	SpeedSuper
	SpeedSuperPlus
)

func (Speed) String

func (s Speed) String() string

Directories

Path Synopsis
_examples
Package gusb contains lower-level USB interaction.
Package gusb contains lower-level USB interaction.

Jump to

Keyboard shortcuts

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