forked from JuulLabs-OSS/cbgo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cutil.go
173 lines (143 loc) · 3.75 KB
/
cutil.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
package cbgo
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Foundation -framework CoreBluetooth
#import "bt.h"
*/
import "C"
import (
"unsafe"
)
func mallocArr(numElems int, elemSize uintptr) unsafe.Pointer {
return C.malloc(C.size_t(numElems) * C.size_t(elemSize))
}
// getArrElemAddr retrieves the address of an element from a C array.
func getArrElemAddr(arr unsafe.Pointer, elemSize uintptr, idx int) unsafe.Pointer {
base := uintptr(arr)
off := uintptr(idx) * elemSize
cur := base + off
return unsafe.Pointer(cur)
}
// freeArrElems frees each element of a C-array of pointers.
func freeArrElems(arr unsafe.Pointer, elemSize uintptr, count int) {
for i := 0; i < count; i++ {
base := uintptr(arr)
off := uintptr(i) * elemSize
addr := base + off
pp := unsafe.Pointer(addr)
ptr := *(*unsafe.Pointer)(pp)
C.free(ptr)
}
}
func byteArrToByteSlice(byteArr *C.struct_byte_arr) []byte {
return C.GoBytes(unsafe.Pointer(byteArr.data), byteArr.length)
}
func byteSliceToByteArr(b []byte) C.struct_byte_arr {
if len(b) == 0 {
return C.struct_byte_arr{
data: nil,
length: 0,
}
} else {
return C.struct_byte_arr{
data: (*C.uint8_t)(C.CBytes(b)),
length: C.int(len(b)),
}
}
}
func stringArrToStringSlice(sa *C.struct_string_arr) []string {
ss := make([]string, sa.count)
for i, _ := range ss {
ss[i] = getStrArrElem(sa, i)
}
return ss
}
func stringSliceToArr(ss []string) C.struct_string_arr {
if len(ss) == 0 {
return C.struct_string_arr{}
}
ptr := mallocArr(len(ss), unsafe.Sizeof((*C.char)(nil)))
carr := (*[1<<30 - 1]*C.char)(ptr)
for i, s := range ss {
carr[i] = C.CString(s)
}
return C.struct_string_arr{
strings: (**C.char)(ptr),
count: C.int(len(ss)),
}
}
// getStrArrElem retrieves an element from a `struct string_arr` C object.
func getStrArrElem(sa *C.struct_string_arr, idx int) string {
elemSize := unsafe.Sizeof(*sa.strings)
ptr := getArrElemAddr(unsafe.Pointer(sa.strings), elemSize, idx)
cstr := *(**C.char)(ptr)
return C.GoString(cstr)
}
func uuidsToStrArr(uuids []UUID) C.struct_string_arr {
var ss []string
for _, u := range uuids {
ss = append(ss, u.String())
}
return stringSliceToArr(ss)
}
func strArrToUUIDs(sa *C.struct_string_arr) ([]UUID, error) {
if sa == nil || sa.count == 0 {
return nil, nil
}
uuids := make([]UUID, sa.count)
ss := stringArrToStringSlice(sa)
for i, s := range ss {
uuid, err := ParseUUID(s)
if err != nil {
return nil, err
}
uuids[i] = uuid
}
return uuids, nil
}
func mustStrArrToUUIDs(sa *C.struct_string_arr) []UUID {
uuids, err := strArrToUUIDs(sa)
if err != nil {
panic(err)
}
return uuids
}
func freeStrArr(sa *C.struct_string_arr) {
freeArrElems(unsafe.Pointer(sa.strings), unsafe.Sizeof(*sa.strings), int(sa.count))
C.free(unsafe.Pointer(sa.strings))
}
func btErrorToNSError(e *C.struct_bt_error) error {
if e == nil || e.msg == nil {
return nil
} else {
return &NSError{
msg: C.GoString(e.msg),
code: int(e.code),
}
}
}
func mallocObjArr(count int) C.struct_obj_arr {
if count <= 0 {
return C.struct_obj_arr{
objs: nil,
count: 0,
}
}
data := mallocArr(count, unsafe.Sizeof(unsafe.Pointer(nil)))
return C.struct_obj_arr{
objs: (*unsafe.Pointer)(data),
count: C.int(count),
}
}
// getStrArrElem retrieves an element from a `struct obj_arr` C object.
func getObjArrElem(oa *C.struct_obj_arr, idx int) unsafe.Pointer {
addr := getArrElemAddr(unsafe.Pointer(oa.objs), unsafe.Sizeof(*oa.objs), idx)
dptr := (*unsafe.Pointer)(addr)
return *dptr
}
// setStrArrElem assigns an element in a `struct obj_arr` C object.
func setObjArrElem(oa *C.struct_obj_arr, idx int, val unsafe.Pointer) {
addr := getArrElemAddr(unsafe.Pointer(oa.objs), unsafe.Sizeof(*oa.objs), idx)
dptr := (*unsafe.Pointer)(addr)
*dptr = val
}