Skip to content

Commit

Permalink
Merge pull request #44 from holiman/updates
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman authored Feb 6, 2024
2 parents 9c14560 + 80d2c4b commit 0963553
Show file tree
Hide file tree
Showing 239 changed files with 36,226 additions and 32,519 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/*
57 changes: 57 additions & 0 deletions demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// This file is originally from https://github.com/karalabe/usb
//
// usb - Self contained USB and HID library for Go
// Copyright 2019 The library Authors
//
// This library is free software: you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// The library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along
// with the library. If not, see <http://www.gnu.org/licenses/>.

//go:build none
// +build none

package main

import (
"fmt"
"strings"
"github.com/karalabe/hid"
)

func main() {
// Enumerate all the HID devices in alphabetical path order
hids, err := hid.Enumerate(0, 0)
if err != nil {
panic(err)
}
for i := 0; i < len(hids); i++ {
for j := i + 1; j < len(hids); j++ {
if hids[i].Path > hids[j].Path {
hids[i], hids[j] = hids[j], hids[i]
}
}
}
for i, hid := range hids {
fmt.Println(strings.Repeat("-", 128))
fmt.Printf("HID #%d\n", i)
fmt.Printf(" OS Path: %s\n", hid.Path)
fmt.Printf(" Vendor ID: %#04x\n", hid.VendorID)
fmt.Printf(" Product ID: %#04x\n", hid.ProductID)
fmt.Printf(" Release: %d\n", hid.Release)
fmt.Printf(" Serial: %s\n", hid.Serial)
fmt.Printf(" Manufacturer: %s\n", hid.Manufacturer)
fmt.Printf(" Product: %s\n", hid.Product)
fmt.Printf(" Usage Page: %#04x\n", hid.UsagePage)
fmt.Printf(" Usage: %d\n", hid.Usage)
fmt.Printf(" Interface: %d\n", hid.Interface)
}
fmt.Println(strings.Repeat("=", 128))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/karalabe/hid

go 1.12
go 1.19
37 changes: 36 additions & 1 deletion hid.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var ErrDeviceClosed = errors.New("hid: device closed")
// operating system is not supported by the library.
var ErrUnsupportedPlatform = errors.New("hid: unsupported platform")

// DeviceInfo is a hidapi info structure.
// DeviceInfo contains all the information we know about a USB device.
type DeviceInfo struct {
Path string // Platform-specific device path
VendorID uint16 // Device Vendor ID
Expand All @@ -35,3 +35,38 @@ type DeviceInfo struct {
// only if the device contains more than one interface.
Interface int
}

// Device is a generic USB device interface. It may either be backed by a USB HID
// device or a low level raw (libusb) device.
type Device interface {
// Close releases the USB device handle.
Close() error

// Write sends a binary blob to a USB device. For HID devices write uses reports,
// for low level USB write uses interrupt transfers.
Write(b []byte) (int, error)

// Read retrieves a binary blob from a USB device. For HID devices read uses
// reports, for low level USB read uses interrupt transfers.
Read(b []byte) (int, error)

// GetFeatureReport retreives a feature report from a HID device
//
// Set the first byte of []b to the Report ID of the report to be read. Make
// sure to allow space for this extra byte in []b. Upon return, the first byte
// will still contain the Report ID, and the report data will start in b[1].
GetFeatureReport(b []byte) (int, error)

// SendFeatureReport sends a feature report to a HID device
//
// Feature reports are sent over the Control endpoint as a Set_Report transfer.
// The first byte of b must contain the Report ID. For devices which only
// support a single report, this must be set to 0x0. The remaining bytes
// contain the report data. Since the Report ID is mandatory, calls to
// SendFeatureReport() will always contain one more byte than the report
// contains. For example, if a hid report is 16 bytes long, 17 bytes must be
// passed to SendFeatureReport(): the Report ID (or 0x0, for devices
// which do not use numbered reports), followed by the report data (16 bytes).
// In this example, the length passed in would be 17.
SendFeatureReport(b []byte) (int, error)
}
49 changes: 19 additions & 30 deletions hid_disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
// This file is released under the 3-clause BSD license. Note however that Linux
// support depends on libusb, released under GNU LGPL 2.1 or later.

// +build !linux,!darwin,!windows ios !cgo
//go:build (!freebsd && !linux && !darwin && !windows) || ios || !cgo
// +build !freebsd,!linux,!darwin,!windows ios !cgo

package hid

Expand All @@ -18,13 +19,13 @@ func Supported() bool {
// Enumerate returns a list of all the HID devices attached to the system which
// match the vendor and product id. On platforms that this file implements the
// function is a noop and returns an empty list always.
func Enumerate(vendorID uint16, productID uint16) []DeviceInfo {
return nil
func Enumerate(vendorID uint16, productID uint16) ([]DeviceInfo, error) {
return nil, nil
}

// Device is a live HID USB connected device handle. On platforms that this file
// implements the type lacks the actual HID device and all methods are noop.
type Device struct {
// hidDevice is a live HID USB connected device handle. On platforms that this file
// implements, the type lacks the actual HID device and all methods are noop.
type hidDevice struct {
DeviceInfo // Embed the infos for easier access
}

Expand All @@ -34,42 +35,30 @@ func (info DeviceInfo) Open() (*Device, error) {
return nil, ErrUnsupportedPlatform
}

// Close releases the HID USB device handle. On platforms that this file implements
// Close releases the HID USB device handle. On platforms that this file implements,
// the method is just a noop.
func (dev *Device) Close() error { return nil }
func (dev *hidDevice) Close() error {
return ErrUnsupportedPlatform
}

// Write sends an output report to a HID device. On platforms that this file
// implements the method just returns an error.
func (dev *Device) Write(b []byte) (int, error) {
// implements, the method just returns an error.
func (dev *hidDevice) Write(b []byte) (int, error) {
return 0, ErrUnsupportedPlatform
}

// SendFeatureReport sends a feature report to a HID device
//
// Feature reports are sent over the Control endpoint as a Set_Report transfer.
// The first byte of b must contain the Report ID. For devices which only
// support a single report, this must be set to 0x0. The remaining bytes
// contain the report data. Since the Report ID is mandatory, calls to
// SendFeatureReport() will always contain one more byte than the report
// contains. For example, if a hid report is 16 bytes long, 17 bytes must be
// passed to SendFeatureReport(): the Report ID (or 0x0, for devices
// which do not use numbered reports), followed by the report data (16 bytes).
// In this example, the length passed in would be 17.
func (dev *Device) SendFeatureReport(b []byte) (int, error) {
// Read retrieves an input report from a HID device. On platforms that this file
// implements, the method just returns an error.
func (dev *hidDevice) Read(b []byte) (int, error) {
return 0, ErrUnsupportedPlatform
}

// Read retrieves an input report from a HID device. On platforms that this file
// implements the method just returns an error.
func (dev *Device) Read(b []byte) (int, error) {
// SendFeatureReport sends a feature report to a HID device
func (dev *hidDevice) SendFeatureReport(b []byte) (int, error) {
return 0, ErrUnsupportedPlatform
}

// GetFeatureReport retreives a feature report from a HID device
//
// Set the first byte of []b to the Report ID of the report to be read. Make
// sure to allow space for this extra byte in []b. Upon return, the first byte
// will still contain the Report ID, and the report data will start in b[1].
func (dev *Device) GetFeatureReport(b []byte) (int, error) {
func (dev *hidDevice) GetFeatureReport(b []byte) (int, error) {
return 0, ErrUnsupportedPlatform
}
Loading

0 comments on commit 0963553

Please sign in to comment.