forked from karalabe/hid
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhid_libusb_linux.go
42 lines (34 loc) · 1.07 KB
/
hid_libusb_linux.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
//go:build !hidraw && cgo
package hid
/*
#include "hidapi/libusb/hidapi_libusb.h"
*/
import "C"
import (
"errors"
"unsafe"
_ "github.com/bearsh/hid/hidapi"
_ "github.com/bearsh/hid/hidapi/libusb"
_ "github.com/bearsh/hid/libusb/libusb"
_ "github.com/bearsh/hid/libusb/libusb/os"
)
// WrapSysDevice opens a HID device using a platform-specific file descriptor that can be recognized by libusb
func WrapSysDevice(sysDev int64, ifNumber int) (*Device, error) {
device := C.hid_libusb_wrap_sys_device(C.intptr_t(sysDev), C.int(ifNumber))
if device == nil {
return nil, errors.New("hidapi: failed to open device")
}
return &Device{
device: device,
}, nil
}
// in above function, the correct argument type for sysDev would be a uintptr but generating
// java binding does not support uintptr. so we add a 'static' check to make sure the size
// of an int is not smaller than the size of an uintptr.
// see also https://commaok.xyz/post/compile-time-assertions/
func uintToSmall()
func init() {
if unsafe.Sizeof(int64(0)) < unsafe.Sizeof(C.intptr_t(0)) {
uintToSmall()
}
}