-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathsyncio.go
More file actions
204 lines (193 loc) · 4.21 KB
/
syncio.go
File metadata and controls
204 lines (193 loc) · 4.21 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
// 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 "unsafe"
// BulkTransfer implements libusb_bulk_transfer to perform a USB bulk transfer.
func (dh *DeviceHandle) BulkTransfer(
endpoint endpointAddress,
data []byte,
length int,
timeout int,
) (int, error) {
var transferred C.int
var dataPtr *C.uchar
if len(data) > 0 {
dataPtr = (*C.uchar)(unsafe.Pointer(&data[0]))
}
err := C.libusb_bulk_transfer(
dh.libusbDeviceHandle,
C.uchar(endpoint),
dataPtr,
C.int(length),
&transferred,
C.uint(timeout),
)
if err != 0 {
return 0, ErrorCode(err)
}
return int(transferred), nil
}
// BulkTransferOut is a helper method that performs a USB bulk output transfer.
func (dh *DeviceHandle) BulkTransferOut(
endpoint endpointAddress,
data []byte,
timeout int,
) (int, error) {
return dh.BulkTransfer(
endpoint,
data,
len(data),
timeout,
)
}
// BulkTransferIn is a helper method that performs a USB bulk input transfer.
func (dh *DeviceHandle) BulkTransferIn(
endpoint endpointAddress,
maxReceiveBytes int,
timeout int,
) ([]byte, int, error) {
data := make([]byte, maxReceiveBytes)
transferred, err := dh.BulkTransfer(
endpoint,
data,
maxReceiveBytes,
timeout,
)
if err != nil {
return nil, 0, err
}
return data, int(transferred), nil
}
// ControlTransfer sends a transfer using a control endpoint for the given
// device handle.
func (dh *DeviceHandle) ControlTransfer(
requestType byte,
request byte,
value uint16,
index uint16,
data []byte,
length int,
timeout int,
) (int, error) {
if dh == nil || dh.libusbDeviceHandle == nil {
return 0, ErrorCode(errorInvalidParam)
}
var dataPtr *C.uchar
if len(data) > 0 {
dataPtr = (*C.uchar)(unsafe.Pointer(&data[0]))
}
ret := C.libusb_control_transfer(
dh.libusbDeviceHandle,
C.uint8_t(requestType),
C.uint8_t(request),
C.uint16_t(value),
C.uint16_t(index),
dataPtr,
C.uint16_t(length),
C.uint(timeout),
)
if ret < 0 {
return 0, ErrorCode(ret)
}
return int(ret), nil
}
// ControlTransferWithTypes is a more type-safe version of ControlTransfer that accepts
// TransferDirection, RequestType, and RequestRecipient components to build the bmRequestType.
// This allows for more readable and maintainable code when using constant values.
func (dh *DeviceHandle) ControlTransferWithTypes(
direction TransferDirection,
reqType RequestType,
recipient RequestRecipient,
request byte,
value uint16,
index uint16,
data []byte,
length int,
timeout int,
) (int, error) {
requestType := BitmapRequestType(direction, reqType, recipient)
return dh.ControlTransfer(
requestType,
request,
value,
index,
data,
length,
timeout,
)
}
// ControlOut is a helper method for control OUT transfers (host to device)
func (dh *DeviceHandle) ControlOut(
reqType RequestType,
recipient RequestRecipient,
request byte,
value uint16,
index uint16,
data []byte,
timeout int,
) (int, error) {
return dh.ControlTransferWithTypes(
HostToDevice,
reqType,
recipient,
request,
value,
index,
data,
len(data),
timeout,
)
}
// ControlIn is a helper method for control IN transfers (device to host)
func (dh *DeviceHandle) ControlIn(
reqType RequestType,
recipient RequestRecipient,
request byte,
value uint16,
index uint16,
data []byte,
maxReceiveLength int,
timeout int,
) (int, error) {
return dh.ControlTransferWithTypes(
DeviceToHost,
reqType,
recipient,
request,
value,
index,
data,
maxReceiveLength,
timeout,
)
}
// InterruptTransfer performs a USB interrupt transfer.
func (dh *DeviceHandle) InterruptTransfer(
endpoint endpointAddress,
data []byte,
length int,
timeout int,
) (int, error) {
var transferred C.int
var dataPtr *C.uchar
if len(data) > 0 {
dataPtr = (*C.uchar)(unsafe.Pointer(&data[0]))
}
err := C.libusb_interrupt_transfer(
dh.libusbDeviceHandle,
C.uchar(endpoint),
dataPtr,
C.int(length),
&transferred,
C.uint(timeout),
)
if err != 0 {
return 0, ErrorCode(err)
}
return int(transferred), nil
}