forked from JuulLabs-OSS/cbgo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
characteristic.go
80 lines (66 loc) · 3.11 KB
/
characteristic.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
package cbgo
import "unsafe"
/*
// See cutil.go for C compiler flags.
#import "bt.h"
*/
import "C"
// CharacteristicProperties: https://developer.apple.com/documentation/corebluetooth/cbcharacteristicproperties
type CharacteristicProperties int
const (
CharacteristicPropertyBroadcast = CharacteristicProperties(C.CBCharacteristicPropertyBroadcast)
CharacteristicPropertyRead = CharacteristicProperties(C.CBCharacteristicPropertyRead)
CharacteristicPropertyWriteWithoutResponse = CharacteristicProperties(C.CBCharacteristicPropertyWriteWithoutResponse)
CharacteristicPropertyWrite = CharacteristicProperties(C.CBCharacteristicPropertyWrite)
CharacteristicPropertyNotify = CharacteristicProperties(C.CBCharacteristicPropertyNotify)
CharacteristicPropertyIndicate = CharacteristicProperties(C.CBCharacteristicPropertyIndicate)
CharacteristicPropertyAuthenticatedSignedWrites = CharacteristicProperties(C.CBCharacteristicPropertyAuthenticatedSignedWrites)
CharacteristicPropertyExtendedProperties = CharacteristicProperties(C.CBCharacteristicPropertyExtendedProperties)
CharacteristicPropertyNotifyEncryptionRequired = CharacteristicProperties(C.CBCharacteristicPropertyNotifyEncryptionRequired)
CharacteristicPropertyIndicateEncryptionRequired = CharacteristicProperties(C.CBCharacteristicPropertyIndicateEncryptionRequired)
)
func chrWriteType(withRsp bool) C.int {
if withRsp {
return C.CBCharacteristicWriteWithResponse
} else {
return C.CBCharacteristicWriteWithoutResponse
}
}
// Characteristic: https://developer.apple.com/documentation/corebluetooth/cbcharacteristic
type Characteristic struct {
ptr unsafe.Pointer
}
// UUID: https://developer.apple.com/documentation/corebluetooth/cbattribute/1620638-uuid
func (c Characteristic) UUID() UUID {
cstr := C.cb_chr_uuid(c.ptr)
return MustParseUUID(C.GoString(cstr))
}
// Service: https://developer.apple.com/documentation/corebluetooth/cbcharacteristic/1518728-service
func (c Characteristic) Service() Service {
ptr := C.cb_chr_service(c.ptr)
return Service{ptr}
}
// Value: https://developer.apple.com/documentation/corebluetooth/cbcharacteristic/1518878-value
func (c Characteristic) Value() []byte {
ba := C.cb_chr_value(c.ptr)
return byteArrToByteSlice(&ba)
}
// Descriptors: https://developer.apple.com/documentation/corebluetooth/cbcharacteristic/1518957-descriptors
func (c Characteristic) Descriptors() []Descriptor {
oa := C.cb_chr_descriptors(c.ptr)
defer C.free(unsafe.Pointer(oa.objs))
dscs := make([]Descriptor, oa.count)
for i, _ := range dscs {
obj := getObjArrElem(&oa, i)
dscs[i] = Descriptor{ptr: obj}
}
return dscs
}
// Properties: https://developer.apple.com/documentation/corebluetooth/cbcharacteristic/1519010-properties
func (c Characteristic) Properties() CharacteristicProperties {
return CharacteristicProperties(C.cb_chr_properties(c.ptr))
}
// IsNotifying: https://developer.apple.com/documentation/corebluetooth/cbcharacteristic/1519057-isnotifying
func (c Characteristic) IsNotifying() bool {
return bool(C.cb_chr_is_notifying(c.ptr))
}