forked from karalabe/usb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb_enabled.go
98 lines (85 loc) · 3.61 KB
/
usb_enabled.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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/>.
// +build freebsd,cgo linux,cgo darwin,!ios,cgo windows,cgo
package usb
import (
"sync"
)
// enumerateLock is a mutex serializing access to USB device enumeration needed
// by the macOS USB HID system calls, which require 2 consecutive method calls
// for enumeration, causing crashes if called concurrently.
//
// For more details, see:
// https://developer.apple.com/documentation/iokit/1438371-iohidmanagersetdevicematching
// > "subsequent calls will cause the hid manager to release previously enumerated devices"
var enumerateLock sync.Mutex
// Supported returns whether this platform is supported by the USB library or not.
// The goal of this method is to allow programatically handling platforms that do
// not support USB and not having to fall back to build constraints.
func Supported() bool {
return true
}
// Enumerate returns a list of all the USB devices attached to the system which
// match the vendor and product id:
// - If the vendor id is set to 0 then any vendor matches.
// - If the product id is set to 0 then any product matches.
// - If the vendor and product id are both 0, all devices are returned.
//
// For any device that is HID capable, the enumeration will return an interface
// to the HID endpoints. For pure raw USB access, please use EnumerateRaw.
func Enumerate(vendorID uint16, productID uint16) ([]DeviceInfo, error) {
enumerateLock.Lock()
defer enumerateLock.Unlock()
// Enumerate all the raw USB devices and skip the HID ones
raws, err := enumerateRaw(vendorID, productID)
if err != nil {
return nil, err
}
// Enumerate all the HID USB devices
hids, err := enumerateHid(vendorID, productID)
if err != nil {
return nil, err
}
return append(raws, hids...), nil
}
// EnumerateRaw returns a list of all the USB devices attached to the system which
// match the vendor and product id:
// - If the vendor id is set to 0 then any vendor matches.
// - If the product id is set to 0 then any product matches.
// - If the vendor and product id are both 0, all devices are returned.
func EnumerateRaw(vendorID uint16, productID uint16) ([]DeviceInfo, error) {
enumerateLock.Lock()
defer enumerateLock.Unlock()
return enumerateRaw(vendorID, productID)
}
// EnumerateHid returns a list of all the HID devices attached to the system which
// match the vendor and product id:
// - If the vendor id is set to 0 then any vendor matches.
// - If the product id is set to 0 then any product matches.
// - If the vendor and product id are both 0, all devices are returned.
func EnumerateHid(vendorID uint16, productID uint16) ([]DeviceInfo, error) {
enumerateLock.Lock()
defer enumerateLock.Unlock()
return enumerateHid(vendorID, productID)
}
// Open connects to a previsouly discovered USB device.
func (info DeviceInfo) Open() (Device, error) {
enumerateLock.Lock()
defer enumerateLock.Unlock()
if info.rawDevice == nil {
return openHid(info)
}
return openRaw(info)
}