/
gatt.go
115 lines (91 loc) · 2.89 KB
/
gatt.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
package ble
import (
"fmt"
)
const (
// GATTMTU is the maximum size of a write to a GATT characteristic.
GATTMTU = 20
serviceInterface = "org.bluez.GattService1"
characteristicInterface = "org.bluez.GattCharacteristic1"
descriptorInterface = "org.bluez.GattDescriptor1"
)
func (conn *Connection) findGattObject(iface string, uuid string) (*blob, error) {
handle, err := conn.findObject(iface, func(desc *blob) bool {
return desc.UUID() == uuid
})
if err != nil {
err = fmt.Errorf("%w with UUID %s", err, uuid)
}
return handle, err
}
// GattHandle is the interface satisfied by GATT handles.
type GattHandle interface {
BaseObject
UUID() string
}
// UUID returns the handle's UUID
func (handle *blob) UUID() string {
return handle.properties["UUID"].Value().(string)
}
// Service corresponds to the org.bluez.GattService1 interface.
// See bluez/doc/gatt-api.txt
type Service interface {
GattHandle
}
// GetService finds a Service with the given UUID.
func (conn *Connection) GetService(uuid string) (Service, error) {
return conn.findGattObject(serviceInterface, uuid)
}
// ReadWriteHandle is the interface satisfied by GATT objects
// that provide ReadValue and WriteValue operations.
type ReadWriteHandle interface {
GattHandle
ReadValue() ([]byte, error)
WriteValue([]byte) error
}
// ReadValue reads the handle's value.
func (handle *blob) ReadValue() ([]byte, error) {
var data []byte
err := handle.callv("ReadValue", Properties{}).Store(&data)
return data, err
}
// WriteValue writes a value to the handle.
func (handle *blob) WriteValue(data []byte) error {
return handle.call("WriteValue", data, Properties{})
}
// NotifyHandler represents a function that handles notifications.
type NotifyHandler func([]byte)
// Characteristic corresponds to the org.bluez.GattCharacteristic1 interface.
// See bluez/doc/gatt-api.txt
type Characteristic interface {
ReadWriteHandle
Notifying() bool
StartNotify() error
StopNotify() error
HandleNotify(NotifyHandler) error
}
// GetCharacteristic finds a Characteristic with the given UUID.
func (conn *Connection) GetCharacteristic(uuid string) (Characteristic, error) {
return conn.findGattObject(characteristicInterface, uuid)
}
// Notifying returns whether or not a Characteristic is notifying.
func (handle *blob) Notifying() bool {
return handle.properties["Notifying"].Value().(bool)
}
// StartNotify starts notifying.
func (handle *blob) StartNotify() error {
return handle.call("StartNotify")
}
// StartNotify stops notifying.
func (handle *blob) StopNotify() error {
return handle.call("StopNotify")
}
// Descriptor corresponds to the org.bluez.GattDescriptor1 interface.
// See bluez/doc/gatt-api.txt
type Descriptor interface {
ReadWriteHandle
}
// GetDescriptor finds a Descriptor with the given UUID.
func (conn *Connection) GetDescriptor(uuid string) (Descriptor, error) {
return conn.findGattObject(descriptorInterface, uuid)
}