forked from currantlabs/ble
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatt.go
187 lines (162 loc) · 4.36 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package ble
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/pkg/errors"
)
// ErrDefaultDevice ...
var ErrDefaultDevice = errors.New("default device is not set")
var defaultDevice Device
// SetDefaultDevice returns the default HCI device.
func SetDefaultDevice(d Device) {
defaultDevice = d
}
// AddService adds a service to database.
func AddService(svc *Service) error {
if defaultDevice == nil {
return ErrDefaultDevice
}
return defaultDevice.AddService(svc)
}
// RemoveAllServices removes all services that are currently in the database.
func RemoveAllServices() error {
if defaultDevice == nil {
return ErrDefaultDevice
}
return defaultDevice.RemoveAllServices()
}
// SetServices set the specified service to the database.
// It removes all currently added services, if any.
func SetServices(svcs []*Service) error {
if defaultDevice == nil {
return ErrDefaultDevice
}
return defaultDevice.SetServices(svcs)
}
// Stop detatch the GATT server from a peripheral device.
func Stop() error {
if defaultDevice == nil {
return ErrDefaultDevice
}
return defaultDevice.Stop()
}
// AdvertiseNameAndServices advertises device name, and specified service UUIDs.
// It tres to fit the UUIDs in the advertising packet as much as possi
// If name doesn't fit in the advertising packet, it will be put in scan response.
func AdvertiseNameAndServices(ctx context.Context, name string, uuids ...UUID) error {
if defaultDevice == nil {
return ErrDefaultDevice
}
defer untrap(trap(ctx))
return defaultDevice.AdvertiseNameAndServices(ctx, name, uuids...)
}
// AdvertiseIBeaconData advertise iBeacon with given manufacturer data.
func AdvertiseIBeaconData(ctx context.Context, b []byte) error {
if defaultDevice == nil {
return ErrDefaultDevice
}
defer untrap(trap(ctx))
return defaultDevice.AdvertiseIBeaconData(ctx, b)
}
// AdvertiseIBeacon advertises iBeacon with specified parameters.
func AdvertiseIBeacon(ctx context.Context, u UUID, major, minor uint16, pwr int8) error {
if defaultDevice == nil {
return ErrDefaultDevice
}
defer untrap(trap(ctx))
return defaultDevice.AdvertiseIBeacon(ctx, u, major, minor, pwr)
}
// Scan starts scanning. Duplicated advertisements will be filtered out if allowDup is set to false.
func Scan(ctx context.Context, allowDup bool, h AdvHandler, f AdvFilter) error {
if defaultDevice == nil {
return ErrDefaultDevice
}
defer untrap(trap(ctx))
if f == nil {
return defaultDevice.Scan(ctx, allowDup, h)
}
h2 := func(a Advertisement) {
if f(a) {
h(a)
}
}
return defaultDevice.Scan(ctx, allowDup, h2)
}
// Find ...
func Find(ctx context.Context, allowDup bool, f AdvFilter) ([]Advertisement, error) {
if defaultDevice == nil {
return nil, ErrDefaultDevice
}
var advs []Advertisement
h := func(a Advertisement) {
advs = append(advs, a)
}
defer untrap(trap(ctx))
return advs, Scan(ctx, allowDup, h, f)
}
// Dial ...
func Dial(ctx context.Context, a Addr) (Client, error) {
if defaultDevice == nil {
return nil, ErrDefaultDevice
}
defer untrap(trap(ctx))
return defaultDevice.Dial(ctx, a)
}
// Connect searches for and connects to a Peripheral which matches specified condition.
func Connect(ctx context.Context, f AdvFilter) (Client, error) {
ctx2, cancel := context.WithCancel(ctx)
go func() {
select {
case <-ctx.Done():
cancel()
case <-ctx2.Done():
}
}()
ch := make(chan Advertisement)
fn := func(a Advertisement) {
cancel()
ch <- a
}
if err := Scan(ctx2, false, fn, f); err != nil {
if err != context.Canceled {
return nil, errors.Wrap(err, "can't scan")
}
}
cln, err := Dial(ctx, (<-ch).Addr())
return cln, errors.Wrap(err, "can't dial")
}
// A NotificationHandler handles notification or indication from a server.
type NotificationHandler func(req []byte)
// WithSigHandler ...
func WithSigHandler(ctx context.Context, cancel func()) context.Context {
return context.WithValue(ctx, ContextKeySig, cancel)
}
// Cleanup for the interrupted case.
func trap(ctx context.Context) chan<- os.Signal {
v := ctx.Value(ContextKeySig)
if v == nil {
return nil
}
cancel, ok := v.(func())
if cancel == nil || !ok {
return nil
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
select {
case <-sigs:
cancel()
case <-ctx.Done():
}
}()
return sigs
}
func untrap(sigs chan<- os.Signal) {
if sigs == nil {
return
}
signal.Stop(sigs)
}