-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathhandle.go
More file actions
284 lines (257 loc) · 7.92 KB
/
handle.go
File metadata and controls
284 lines (257 loc) · 7.92 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (c) 2015-2025 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 (
"runtime"
"unsafe"
)
// DeviceHandle represents the libusb device handle.
type DeviceHandle struct {
libusbDeviceHandle *C.libusb_device_handle
}
// deviceHandleFinalizer is called by the garbage collector to clean up
// unreferenced DeviceHandle objects that weren't explicitly closed.
func deviceHandleFinalizer(dh *DeviceHandle) {
if dh.libusbDeviceHandle != nil {
C.libusb_close(dh.libusbDeviceHandle)
dh.libusbDeviceHandle = nil
}
}
// newDeviceHandle creates a new DeviceHandle with proper finalizer setup.
func newDeviceHandle(libusbDeviceHandle *C.libusb_device_handle) *DeviceHandle {
dh := &DeviceHandle{
libusbDeviceHandle: libusbDeviceHandle,
}
runtime.SetFinalizer(dh, deviceHandleFinalizer)
return dh
}
// StringDescriptor retrieves a descriptor from a device.
func (dh *DeviceHandle) StringDescriptor(
descIndex uint8,
langID uint16,
) (string, error) {
if dh == nil || dh.libusbDeviceHandle == nil {
return "", ErrorCode(errorInvalidParam)
}
// Allocate buffer for data
length := 512
cData := make([]C.uchar, length)
var dataPtr *C.uchar
if len(cData) > 0 {
dataPtr = &cData[0]
}
usberr := C.libusb_get_string_descriptor(
dh.libusbDeviceHandle,
C.uint8_t(descIndex),
C.uint16_t(langID),
dataPtr,
C.int(length),
)
if usberr < 0 {
return "", ErrorCode(usberr)
}
// Convert to Go string
if len(cData) > 0 {
data := (*C.char)(unsafe.Pointer(&cData[0]))
return C.GoString(data), nil
}
return "", 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) {
if dh == nil || dh.libusbDeviceHandle == nil {
return "", ErrorCode(errorInvalidParam)
}
// TODO(mdr): Should the length be a constant? Why did I pick 256 bytes?
length := 256
data := make([]byte, length)
var dataPtr *C.uchar
if len(data) > 0 {
dataPtr = (*C.uchar)(unsafe.Pointer(&data[0]))
}
bytesRead, err := C.libusb_get_string_descriptor_ascii(
dh.libusbDeviceHandle,
C.uint8_t(descIndex),
// Unsafe pointer -> https://stackoverflow.com/a/16376039/95592
dataPtr,
C.int(length),
)
// Check both bytesRead and err
if err != nil {
return "", err
}
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 {
if dh == nil || dh.libusbDeviceHandle == nil {
return ErrorCode(errorInvalidParam)
}
C.libusb_close(dh.libusbDeviceHandle)
dh.libusbDeviceHandle = nil
// Clear finalizer since we've explicitly closed the device handle
runtime.SetFinalizer(dh, nil)
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) {
if dh == nil || dh.libusbDeviceHandle == nil {
return 0, ErrorCode(errorInvalidParam)
}
// Allocate memory for the configuration value
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 {
if dh == nil || dh.libusbDeviceHandle == nil {
return ErrorCode(errorInvalidParam)
}
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 {
if dh == nil || dh.libusbDeviceHandle == nil {
return ErrorCode(errorInvalidParam)
}
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 {
if dh == nil || dh.libusbDeviceHandle == nil {
return ErrorCode(errorInvalidParam)
}
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 {
if dh == nil || dh.libusbDeviceHandle == nil {
return ErrorCode(errorInvalidParam)
}
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 {
if dh == nil || dh.libusbDeviceHandle == nil {
return ErrorCode(errorInvalidParam)
}
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) {
if dh == nil || dh.libusbDeviceHandle == nil {
return false, ErrorCode(errorInvalidParam)
}
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 {
if dh == nil || dh.libusbDeviceHandle == nil {
return ErrorCode(errorInvalidParam)
}
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 {
if dh == nil || dh.libusbDeviceHandle == nil {
return ErrorCode(errorInvalidParam)
}
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 {
if dh == nil || dh.libusbDeviceHandle == nil {
return ErrorCode(errorInvalidParam)
}
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
}