Repository navigation
Expand file tree
/
Copy pathcallbacks.go
More file actions
74 lines (66 loc) · 1.97 KB
/
Copy pathcallbacks.go
File metadata and controls
74 lines (66 loc) · 1.97 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
package hackrf
// This file holds the //export trampolines invoked by libhackrf's transfer
// thread. cgo forbids C function definitions in the preamble of a file that
// contains //export directives, so the C wrappers live in streaming.go and the
// preamble here is limited to the header (declarations only).
// #include <libhackrf/hackrf.h>
import "C"
import (
"runtime/cgo"
"unsafe"
)
// deviceFromCtx recovers the *Device from an opaque user-context void* that
// carries a cgo.Handle value.
func deviceFromCtx(ctx unsafe.Pointer) *Device {
if ctx == nil {
return nil
}
v := cgo.Handle(uintptr(ctx)).Value()
d, _ := v.(*Device)
return d
}
// wrapTransfer builds a Go Transfer aliasing the C buffer.
func wrapTransfer(d *Device, ct *C.hackrf_transfer) *Transfer {
var buf []byte
if ct.buffer != nil && ct.buffer_length > 0 {
buf = unsafe.Slice((*byte)(unsafe.Pointer(ct.buffer)), int(ct.buffer_length))
}
return &Transfer{
Device: d,
Buffer: buf,
BufferLength: int(ct.buffer_length),
ValidLength: int(ct.valid_length),
}
}
//export goHackRFTransferCallback
func goHackRFTransferCallback(ct *C.hackrf_transfer) C.int {
ctx := unsafe.Pointer(ct.rx_ctx)
if ctx == nil {
ctx = unsafe.Pointer(ct.tx_ctx)
}
d := deviceFromCtx(ctx)
if d == nil || d.transferCallback == nil {
return 0
}
t := wrapTransfer(d, ct)
ret := d.transferCallback(t)
// Propagate any TX valid_length change back to libhackrf.
ct.valid_length = C.int(t.ValidLength)
return C.int(ret)
}
//export goHackRFTxBlockCompleteCallback
func goHackRFTxBlockCompleteCallback(ct *C.hackrf_transfer, success C.int) {
d := deviceFromCtx(unsafe.Pointer(ct.tx_ctx))
if d == nil || d.blockCompleteCallback == nil {
return
}
d.blockCompleteCallback(wrapTransfer(d, ct), int(success))
}
//export goHackRFFlushCallback
func goHackRFFlushCallback(flushCtx unsafe.Pointer, success C.int) {
d := deviceFromCtx(flushCtx)
if d == nil || d.flushCallback == nil {
return
}
d.flushCallback(int(success))
}