-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.go
198 lines (180 loc) · 5.7 KB
/
handle.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) 2015-2023 The libusb developers. All rights reserved.
// Project site: https://github.com/gotmc/libusb
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.
package libusb
// #cgo pkg-config: libusb-1.0
// #include <libusb.h>
import "C"
import (
"unsafe"
)
// DeviceHandle represents the libusb device handle.
type DeviceHandle struct {
libusbDeviceHandle *C.libusb_device_handle
}
// StringDescriptor retrieves a descriptor from a device.
func (dh *DeviceHandle) StringDescriptor(
descIndex uint8,
langID uint16,
) (string, error) {
var cData *C.uchar
length := 512
usberr := C.libusb_get_string_descriptor(
dh.libusbDeviceHandle,
C.uint8_t(descIndex),
C.uint16_t(langID),
cData,
C.int(length),
)
if usberr < 0 {
return "", ErrorCode(usberr)
}
data := (*C.char)(unsafe.Pointer(cData))
return C.GoString(data), nil
}
// StringDescriptorASCII retrieve(s) a string descriptor in C style ASCII.
// Wrapper around libusb_get_string_descriptor(). Uses the first language
// supported by the device. (Source: libusb docs)
func (dh *DeviceHandle) StringDescriptorASCII(
descIndex uint8,
) (string, error) {
// TODO(mdr): Should the length be a constant? Why did I pick 256 bytes?
length := 256
data := make([]byte, length)
bytesRead, _ := C.libusb_get_string_descriptor_ascii(
dh.libusbDeviceHandle,
C.uint8_t(descIndex),
// Unsafe pointer -> http://stackoverflow.com/a/16376039/95592
(*C.uchar)(unsafe.Pointer(&data[0])),
C.int(length),
)
if bytesRead < 0 {
return "", ErrorCode(bytesRead)
}
return string(data[0:bytesRead]), nil
}
// Close implements libusb_close to close the device handle.
func (dh *DeviceHandle) Close() error {
C.libusb_close(dh.libusbDeviceHandle)
return nil
}
// Device implements libusb_get_device to get the underlying device for a
// handle.
// TODO(mdr): Determine if I actually need this function.
// func (dh *DeviceHandle) Device() (*Device, error) {
// }
// Configuration implements the libusb_get_configuration function to
// determine the bConfigurationValue of the currently active configuration.
func (dh *DeviceHandle) Configuration() (int, error) {
var configuration *C.int
err := C.libusb_get_configuration(dh.libusbDeviceHandle, configuration)
if err != 0 {
return 0, ErrorCode(err)
}
return int(*configuration), nil
}
// SetConfiguration implements libusb_set_configuration to set the active
// configuration for the device.
func (dh *DeviceHandle) SetConfiguration(configuration int) error {
err := C.libusb_set_configuration(dh.libusbDeviceHandle,
C.int(configuration))
if err != 0 {
return ErrorCode(err)
}
return nil
}
// ClaimInterface implements libusb_claim_interface to claim an interface on a
// given device handle. You must claim the interface you wish to use before you
// can perform I/O on any of its endpoints.
func (dh *DeviceHandle) ClaimInterface(interfaceNum int) error {
err := C.libusb_claim_interface(dh.libusbDeviceHandle, C.int(interfaceNum))
if err != 0 {
return ErrorCode(err)
}
return nil
}
// ReleaseInterface implements libusb_release_interface to release an interface
// previously claimed with libusb_claim_interface() (i.e., ClaimInterface()).
func (dh *DeviceHandle) ReleaseInterface(interfaceNum int) error {
err := C.libusb_release_interface(dh.libusbDeviceHandle, C.int(interfaceNum))
if err != 0 {
return ErrorCode(err)
}
return nil
}
// SetInterfaceAltSetting activates an alternate setting for an interface.
func (dh *DeviceHandle) SetInterfaceAltSetting(
interfaceNum int,
alternateSetting int,
) error {
err := C.libusb_set_interface_alt_setting(
dh.libusbDeviceHandle,
C.int(interfaceNum),
C.int(alternateSetting),
)
if err != 0 {
return ErrorCode(err)
}
return nil
}
// FIXME(mdr): libusb_clear_halt takes an endpoint as an unsigned char. Need to
// determine, what I should pass into this function as the endpoint.
// func (dh *DeviceHandle) ClearHalt(endpoint int) error {
// return nil
// }
// ResetDevice implements libusb_reset_device to perform a USB port reset to
// reinitialize a device.
func (dh *DeviceHandle) ResetDevice() error {
err := C.libusb_reset_device(dh.libusbDeviceHandle)
if err != 0 {
return ErrorCode(err)
}
return nil
}
// KernelDriverActive implements libusb_kernel_driver_active to determine if a
// kernel driver is active on an interface.
func (dh *DeviceHandle) KernelDriverActive(interfaceNum int) (bool, error) {
ret := C.libusb_kernel_driver_active(
dh.libusbDeviceHandle, C.int(interfaceNum))
if ret == 1 {
return true, nil
} else if ret != 0 {
return false, ErrorCode(ret)
}
return false, nil
}
// DetachKernelDriver implements libusb_detach_kernel_driver to detach a kernel
// driver from an interface.
func (dh *DeviceHandle) DetachKernelDriver(interfaceNum int) error {
err := C.libusb_detach_kernel_driver(
dh.libusbDeviceHandle, C.int(interfaceNum))
if err != 0 {
return ErrorCode(err)
}
return nil
}
// AttachKernelDriver implements libusb_attach_kernel_driver to re-attach an
// interface's kernel driver, which was previously detached using
// libusb_detach_kernel_driver().
func (dh *DeviceHandle) AttachKernelDriver(interfaceNum int) error {
err := C.libusb_attach_kernel_driver(
dh.libusbDeviceHandle, C.int(interfaceNum))
if err != 0 {
return ErrorCode(err)
}
return nil
}
// SetAutoDetachKernelDriver implements libusb_set_auto_detach_kernel_driver to
// enable/disable libusb's automatic kernel driver detachment.
func (dh *DeviceHandle) SetAutoDetachKernelDriver(enable bool) error {
cEnable := C.int(0)
if enable {
cEnable = C.int(1)
}
err := C.libusb_set_auto_detach_kernel_driver(dh.libusbDeviceHandle, cEnable)
if err != 0 {
return ErrorCode(err)
}
return nil
}