forked from boombuler/hid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hid_darwin.go
328 lines (294 loc) · 9.64 KB
/
hid_darwin.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package hid
/*
#cgo LDFLAGS: -L . -L/usr/local/lib -framework CoreFoundation -framework IOKit -fconstant-cfstrings
#include <IOKit/hid/IOHIDManager.h>
#include <CoreFoundation/CoreFoundation.h>
static inline CFIndex cfstring_utf8_length(CFStringRef str, CFIndex *need) {
CFIndex n, usedBufLen;
CFRange rng = CFRangeMake(0, CFStringGetLength(str));
return CFStringGetBytes(str, rng, kCFStringEncodingUTF8, 0, 0, NULL, 0, need);
}
void deviceUnpluged(IOHIDDeviceRef osd, IOReturn ret, void* dev);
*/
import "C"
import (
"errors"
"fmt"
"reflect"
"unsafe"
)
func ioReturnToErr(ret C.IOReturn) error {
switch ret {
case C.kIOReturnSuccess:
return nil
case C.kIOReturnError:
return errors.New("general error ")
case C.kIOReturnNoMemory:
return errors.New("can't allocate memory")
case C.kIOReturnNoResources:
return errors.New("resource shortage")
case C.kIOReturnIPCError:
return errors.New("error during IPC")
case C.kIOReturnNoDevice:
return errors.New("no such device")
case C.kIOReturnNotPrivileged:
return errors.New("privilege violation")
case C.kIOReturnBadArgument:
return errors.New("invalid argument")
case C.kIOReturnLockedRead:
return errors.New("device read locked")
case C.kIOReturnLockedWrite:
return errors.New("device write locked")
case C.kIOReturnExclusiveAccess:
return errors.New("exclusive access and device already open")
case C.kIOReturnBadMessageID:
return errors.New("sent/received messages had different msg_id")
case C.kIOReturnUnsupported:
return errors.New("unsupported function")
case C.kIOReturnVMError:
return errors.New("misc. VM failure")
case C.kIOReturnInternalError:
return errors.New("internal error")
case C.kIOReturnIOError:
return errors.New("General I/O error")
case C.kIOReturnCannotLock:
return errors.New("can't acquire lock")
case C.kIOReturnNotOpen:
return errors.New("device not open")
case C.kIOReturnNotReadable:
return errors.New("read not supported")
case C.kIOReturnNotWritable:
return errors.New("write not supported")
case C.kIOReturnNotAligned:
return errors.New("alignment error")
case C.kIOReturnBadMedia:
return errors.New("Media Error")
case C.kIOReturnStillOpen:
return errors.New("device(s) still open")
case C.kIOReturnRLDError:
return errors.New("rld failure")
case C.kIOReturnDMAError:
return errors.New("DMA failure")
case C.kIOReturnBusy:
return errors.New("Device Busy")
case C.kIOReturnTimeout:
return errors.New("I/O Timeout")
case C.kIOReturnOffline:
return errors.New("device offline")
case C.kIOReturnNotReady:
return errors.New("not ready")
case C.kIOReturnNotAttached:
return errors.New("device not attached")
case C.kIOReturnNoChannels:
return errors.New("no DMA channels left")
case C.kIOReturnNoSpace:
return errors.New("no space for data")
case C.kIOReturnPortExists:
return errors.New("port already exists")
case C.kIOReturnCannotWire:
return errors.New("can't wire down physical memory")
case C.kIOReturnNoInterrupt:
return errors.New("no interrupt attached")
case C.kIOReturnNoFrames:
return errors.New("no DMA frames enqueued")
case C.kIOReturnMessageTooLarge:
return errors.New("oversized msg received on interrupt port")
case C.kIOReturnNotPermitted:
return errors.New("not permitted")
case C.kIOReturnNoPower:
return errors.New("no power to device")
case C.kIOReturnNoMedia:
return errors.New("media not present")
case C.kIOReturnUnformattedMedia:
return errors.New("edia not formatted")
case C.kIOReturnUnsupportedMode:
return errors.New("no such mode")
case C.kIOReturnUnderrun:
return errors.New("data underrun")
case C.kIOReturnOverrun:
return errors.New("data overrun")
case C.kIOReturnDeviceError:
return errors.New("the device is not working properly!")
case C.kIOReturnNoCompletion:
return errors.New("a completion routine is required")
case C.kIOReturnAborted:
return errors.New("operation aborted")
case C.kIOReturnNoBandwidth:
return errors.New("bus bandwidth would be exceeded")
case C.kIOReturnNotResponding:
return errors.New("device not responding")
case C.kIOReturnIsoTooOld:
return errors.New("isochronous I/O request for distant past!")
case C.kIOReturnIsoTooNew:
return errors.New("isochronous I/O request for distant future")
case C.kIOReturnNotFound:
return errors.New("data was not found")
}
return errors.New("Unknown error")
}
type cleanupDeviceManagerFn func()
type osxDevice struct {
osDevice C.IOHIDDeviceRef
disconnected bool
closeDM cleanupDeviceManagerFn
}
func cfstring(s string) C.CFStringRef {
n := C.CFIndex(len(s))
return C.CFStringCreateWithBytes(nil, *(**C.UInt8)(unsafe.Pointer(&s)), n, C.kCFStringEncodingUTF8, 0)
}
func gostring(cfs C.CFStringRef) string {
if cfs == nil {
return ""
}
var usedBufLen C.CFIndex
n := C.cfstring_utf8_length(cfs, &usedBufLen)
if n <= 0 {
return ""
}
rng := C.CFRange{location: C.CFIndex(0), length: n}
buf := make([]byte, int(usedBufLen))
bufp := unsafe.Pointer(&buf[0])
C.CFStringGetBytes(cfs, rng, C.kCFStringEncodingUTF8, 0, 0, (*C.UInt8)(bufp), C.CFIndex(len(buf)), &usedBufLen)
sh := &reflect.StringHeader{
Data: uintptr(bufp),
Len: int(usedBufLen),
}
return *(*string)(unsafe.Pointer(sh))
}
func getIntProp(device C.IOHIDDeviceRef, key C.CFStringRef) int32 {
var value int32
ref := C.IOHIDDeviceGetProperty(device, key)
if ref != nil {
if C.CFGetTypeID(ref) == C.CFNumberGetTypeID() {
C.CFNumberGetValue(C.CFNumberRef(ref), C.kCFNumberSInt32Type, unsafe.Pointer(&value))
return value
}
}
return 0
}
func getStringProp(device C.IOHIDDeviceRef, key C.CFStringRef) string {
s := C.IOHIDDeviceGetProperty(device, key)
return gostring(C.CFStringRef(s))
}
func getPath(osDev C.IOHIDDeviceRef) string {
return fmt.Sprintf("%s_%04x_%04x_%08x",
getStringProp(osDev, cfstring(C.kIOHIDTransportKey)),
uint16(getIntProp(osDev, cfstring(C.kIOHIDVendorIDKey))),
uint16(getIntProp(osDev, cfstring(C.kIOHIDProductIDKey))),
uint32(getIntProp(osDev, cfstring(C.kIOHIDLocationIDKey))))
}
func iterateDevices(action func(device C.IOHIDDeviceRef) bool) cleanupDeviceManagerFn {
mgr := C.IOHIDManagerCreate(C.kCFAllocatorDefault, C.kIOHIDOptionsTypeNone)
C.IOHIDManagerSetDeviceMatching(mgr, nil)
C.IOHIDManagerOpen(mgr, C.kIOHIDOptionsTypeNone)
allDevicesSet := C.IOHIDManagerCopyDevices(mgr)
defer C.CFRelease(C.CFTypeRef(allDevicesSet))
devCnt := C.CFSetGetCount(allDevicesSet)
allDevices := make([]unsafe.Pointer, uint64(devCnt))
C.CFSetGetValues(allDevicesSet, &allDevices[0])
for _, pDev := range allDevices {
if !action(C.IOHIDDeviceRef(pDev)) {
break
}
}
return func() {
C.IOHIDManagerClose(mgr, C.kIOHIDOptionsTypeNone)
C.CFRelease(C.CFTypeRef(mgr))
}
}
func Devices() <-chan *DeviceInfo {
result := make(chan *DeviceInfo)
go func() {
iterateDevices(func(device C.IOHIDDeviceRef) bool {
result <- &DeviceInfo{
VendorId: uint16(getIntProp(device, cfstring(C.kIOHIDVendorIDKey))),
ProductId: uint16(getIntProp(device, cfstring(C.kIOHIDProductIDKey))),
VersionNumber: uint16(getIntProp(device, cfstring(C.kIOHIDVersionNumberKey))),
Manufacturer: getStringProp(device, cfstring(C.kIOHIDManufacturerKey)),
Product: getStringProp(device, cfstring(C.kIOHIDProductKey)),
InputReportLength: uint16(getIntProp(device, cfstring(C.kIOHIDMaxInputReportSizeKey))),
OutputReportLength: uint16(getIntProp(device, cfstring(C.kIOHIDMaxOutputReportSizeKey))),
FeatureReportLength: uint16(getIntProp(device, cfstring(C.kIOHIDMaxFeatureReportSizeKey))),
Path: getPath(device),
}
return true
})()
close(result)
}()
return result
}
func ByPath(path string) (*DeviceInfo, error) {
for d := range Devices() {
if d.Path == path {
return d, nil
}
}
return nil, errors.New("Device not found")
}
func (di *DeviceInfo) Open() (Device, error) {
err := errors.New("device not found")
var dev *osxDevice
closeDM := iterateDevices(func(device C.IOHIDDeviceRef) bool {
if getPath(device) == di.Path {
res := C.IOHIDDeviceOpen(device, C.kIOHIDOptionsTypeSeizeDevice)
if res == C.kIOReturnSuccess {
C.CFRetain(C.CFTypeRef(device))
dev = &osxDevice{osDevice: device}
err = nil
C.IOHIDDeviceRegisterRemovalCallback(device, (C.IOHIDCallback)(unsafe.Pointer(C.deviceUnpluged)), unsafe.Pointer(dev))
} else {
err = ioReturnToErr(res)
}
return false
}
return true
})
if dev != nil {
dev.closeDM = closeDM
}
return dev, err
}
//export deviceUnpluged
func deviceUnpluged(osdev C.IOHIDDeviceRef, result C.IOReturn, dev unsafe.Pointer) {
od := (*osxDevice)(dev)
od.disconnected = true
od.Close()
}
func (dev *osxDevice) Close() {
if !dev.disconnected {
C.IOHIDDeviceClose(dev.osDevice, C.kIOHIDOptionsTypeSeizeDevice)
dev.disconnected = true
}
if dev.osDevice != nil {
C.CFRelease(C.CFTypeRef(dev.osDevice))
dev.osDevice = nil
}
if dev.closeDM != nil {
dev.closeDM()
dev.closeDM = nil
}
}
func (dev *osxDevice) setReport(typ C.IOHIDReportType, data []byte) error {
var reportNo int32 = int32(data[0])
if reportNo == 0 {
data = data[1:]
}
if !dev.disconnected {
res := C.IOHIDDeviceSetReport(dev.osDevice, typ, C.CFIndex(reportNo), (*C.uint8_t)(&data[0]), C.CFIndex(len(data)))
if res == C.kIOReturnSuccess {
return nil
} else {
return ioReturnToErr(res)
}
}
return errors.New("device disconnected")
}
func (dev *osxDevice) WriteFeature(data []byte) error {
return dev.setReport(C.kIOHIDReportTypeFeature, data)
}
func (dev *osxDevice) Write(data []byte) error {
return dev.setReport(C.kIOHIDReportTypeOutput, data)
}
func (dev *osxDevice) WriteInterrupt(endpoint byte, data []byte) (int, error) {
return 0, errors.New("WriteInterrupt is not implemented")
}