-
Notifications
You must be signed in to change notification settings - Fork 12
/
peripheralmanager.go
146 lines (116 loc) · 4.93 KB
/
peripheralmanager.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
package cbgo
/*
// See cutil.go for C compiler flags.
#import "bt.h"
*/
import "C"
import (
"unsafe"
)
// PeripheralManagerConnectionLatency: https://developer.apple.com/documentation/corebluetooth/cbperipheralmanagerconnectionlatency
type PeripheralManagerConnectionLatency int
const (
PeripheralManagerConnectionLatencyLow = PeripheralManagerConnectionLatency(C.CBPeripheralManagerConnectionLatencyLow)
PeripheralManagerConnectionLatencyMedium = PeripheralManagerConnectionLatency(C.CBPeripheralManagerConnectionLatencyMedium)
PeripheralManagerConnectionLatencyHigh = PeripheralManagerConnectionLatency(C.CBPeripheralManagerConnectionLatencyHigh)
)
// PeripheralManagerRestoreOpts: https://developer.apple.com/documentation/corebluetooth/cbperipheralmanagerdelegate/peripheral_manager_state_restoration_options
type PeripheralManagerRestoreOpts struct {
Services []MutableService
AdvertisementData *AdvData
}
// PeripheralManager: https://developer.apple.com/documentation/corebluetooth/cbperipheralmanager
type PeripheralManager struct {
ptr unsafe.Pointer
}
var pmgrPtrMap = newPtrMap()
func findPeripheralManagerDlg(ptr unsafe.Pointer) PeripheralManagerDelegate {
itf := pmgrPtrMap.find(ptr)
if itf == nil {
return nil
}
return itf.(PeripheralManagerDelegate)
}
// NewPeripheralManager creates a peripheral manager. Specify a nil `opts`
// value for defaults. Don't forget to call `SetDelegate()` afterwards!
func NewPeripheralManager(opts *ManagerOpts) PeripheralManager {
if opts == nil {
opts = &DfltManagerOpts
}
pwrAlert := C.bool(opts.ShowPowerAlert)
restoreID := (*C.char)(nil)
if opts.RestoreIdentifier != "" {
restoreID = C.CString(opts.RestoreIdentifier)
defer C.free(unsafe.Pointer(restoreID))
}
return PeripheralManager{
ptr: unsafe.Pointer(C.cb_alloc_pmgr(pwrAlert, restoreID)),
}
}
// SetDelegate configures a receiver for a central manager's asynchronous
// callbacks.
func (pm PeripheralManager) SetDelegate(d PeripheralManagerDelegate) {
if d != nil {
pmgrPtrMap.add(pm.ptr, d)
}
C.cb_pmgr_set_delegate(pm.ptr, C.bool(d != nil))
}
// State: https://developer.apple.com/documentation/corebluetooth/cbmanager/1648600-state
func (pm PeripheralManager) State() ManagerState {
return ManagerState(C.cb_pmgr_state(pm.ptr))
}
// AddService: https://developer.apple.com/documentation/corebluetooth/cbperipheralmanager/1393255-addservice
func (pm PeripheralManager) AddService(svc MutableService) {
C.cb_pmgr_add_svc(pm.ptr, svc.ptr)
}
// RemoveService: https://developer.apple.com/documentation/corebluetooth/cbperipheralmanager/1393287-removeservice
func (pm PeripheralManager) RemoveService(svc MutableService) {
C.cb_pmgr_remove_svc(pm.ptr, svc.ptr)
}
// RemoveAllServices: https://developer.apple.com/documentation/corebluetooth/cbperipheralmanager/1393269-removeallservices
func (pm PeripheralManager) RemoveAllServices() {
C.cb_pmgr_remove_all_svcs(pm.ptr)
}
// StartAdvertising: https://developer.apple.com/documentation/corebluetooth/cbperipheralmanager/1393252-startadvertising
func (pm PeripheralManager) StartAdvertising(ad AdvData) {
cad := C.struct_adv_data{}
if len(ad.IBeaconData) > 0 {
cad.ibeacon_data = byteSliceToByteArr(ad.IBeaconData)
defer C.free(unsafe.Pointer(cad.ibeacon_data.data))
} else {
if len(ad.LocalName) > 0 {
cad.name = C.CString(ad.LocalName)
defer C.free(unsafe.Pointer(cad.name))
}
cad.svc_uuids = uuidsToStrArr(ad.ServiceUUIDs)
defer freeStrArr(&cad.svc_uuids)
}
C.cb_pmgr_start_adv(pm.ptr, &cad)
}
// StopAdvertising: https://developer.apple.com/documentation/corebluetooth/cbperipheralmanager/1393275-stopadvertising
func (pm PeripheralManager) StopAdvertising() {
C.cb_pmgr_stop_adv(pm.ptr)
}
// IsAdvertising: https://developer.apple.com/documentation/corebluetooth/cbperipheralmanager/1393291-isadvertising
func (pm PeripheralManager) IsAdvertising() bool {
return bool(C.cb_pmgr_is_adv(pm.ptr))
}
// UpdateValue: https://developer.apple.com/documentation/corebluetooth/cbperipheralmanager/1393281-updatevalue
func (pm PeripheralManager) UpdateValue(value []byte, chr Characteristic, centrals []Central) bool {
ba := byteSliceToByteArr(value)
defer C.free(unsafe.Pointer(ba.data))
oa := mallocObjArr(len(centrals))
defer C.free(unsafe.Pointer(oa.objs))
for i, c := range centrals {
setObjArrElem(&oa, i, c.ptr)
}
return bool(C.cb_pmgr_update_val(pm.ptr, &ba, chr.ptr, &oa))
}
// RespondToRequest: https://developer.apple.com/documentation/corebluetooth/cbperipheralmanager/1393293-respondtorequest
func (pm PeripheralManager) RespondToRequest(req ATTRequest, result ATTError) {
C.cb_pmgr_respond_to_req(pm.ptr, req.ptr, C.int(result))
}
// SetDesiredConnectionLatency: https://developer.apple.com/documentation/corebluetooth/cbperipheralmanager/1393277-setdesiredconnectionlatency
func (pm PeripheralManager) SetDesiredConnectionLatency(latency PeripheralManagerConnectionLatency, central Central) {
C.cb_pmgr_set_conn_latency(pm.ptr, C.int(latency), central.ptr)
}