/
db.go
210 lines (182 loc) · 4.46 KB
/
db.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package att
import (
"encoding/binary"
"fmt"
"github.com/AWildBeard/ble"
)
// A DB is a contiguous range of attributes.
type DB struct {
attrs []*attr
base uint16 // handle for first attr in attrs
}
const (
tooSmall = -1
tooLarge = -2
)
// idx returns the idx into attrs corresponding to attr a.
// If h is too small, idx returns tooSmall (-1).
// If h is too large, idx returns tooLarge (-2).
func (r *DB) idx(h int) int {
if h < int(r.base) {
return tooSmall
}
if h >= int(r.base)+len(r.attrs) {
return tooLarge
}
return h - int(r.base)
}
// at returns attr a.
func (r *DB) at(h uint16) (a *attr, ok bool) {
i := r.idx(int(h))
if i < 0 {
return nil, false
}
return r.attrs[i], true
}
// subrange returns attributes in range [start, end]; it may return an empty slice.
// subrange does not panic for out-of-range start or end.
func (r *DB) subrange(start, end uint16) []*attr {
startidx := r.idx(int(start))
switch startidx {
case tooSmall:
startidx = 0
case tooLarge:
return []*attr{}
}
endidx := r.idx(int(end) + 1) // [start, end] includes its upper bound!
switch endidx {
case tooSmall:
return []*attr{}
case tooLarge:
endidx = len(r.attrs)
}
return r.attrs[startidx:endidx]
}
// NewDB ...
func NewDB(ss []*ble.Service, base uint16) *DB {
h := base
var attrs []*attr
var aa []*attr
for i, s := range ss {
h, aa = genSvcAttr(s, h)
if i == len(ss)-1 {
aa[0].endh = 0xFFFF
}
attrs = append(attrs, aa...)
}
DumpAttributes(attrs)
return &DB{attrs: attrs, base: base}
}
func genSvcAttr(s *ble.Service, h uint16) (uint16, []*attr) {
a := &attr{
h: h,
typ: ble.PrimaryServiceUUID,
v: s.UUID,
}
h++
attrs := []*attr{a}
var aa []*attr
for _, c := range s.Characteristics {
h, aa = genCharAttr(c, h)
attrs = append(attrs, aa...)
}
a.endh = h - 1
return h, attrs
}
func genCharAttr(c *ble.Characteristic, h uint16) (uint16, []*attr) {
vh := h + 1
a := &attr{
h: h,
typ: ble.CharacteristicUUID,
v: append([]byte{byte(c.Property), byte(vh), byte((vh) >> 8)}, c.UUID...),
}
va := &attr{
h: vh,
typ: c.UUID,
v: c.Value,
rh: c.ReadHandler,
wh: c.WriteHandler,
}
c.Handle = h
c.ValueHandle = vh
if c.NotifyHandler != nil || c.IndicateHandler != nil {
c.CCCD = newCCCD(c)
c.Descriptors = append(c.Descriptors, c.CCCD)
}
h += 2
attrs := []*attr{a, va}
for _, d := range c.Descriptors {
attrs = append(attrs, genDescAttr(d, h))
h++
}
a.endh = h - 1
return h, attrs
}
func genDescAttr(d *ble.Descriptor, h uint16) *attr {
return &attr{
h: h,
typ: d.UUID,
v: d.Value,
rh: d.ReadHandler,
wh: d.WriteHandler,
}
}
// DumpAttributes ...
func DumpAttributes(aa []*attr) {
logger.Debug("server", "db", "Generating attribute table:")
logger.Debug("server", "db", "handle endh type")
for _, a := range aa {
if a.v != nil {
logger.Debug("server", "db", fmt.Sprintf("0x%04X 0x%04X 0x%s [% X]", a.h, a.endh, a.typ, a.v))
continue
}
logger.Debug("server", "db", fmt.Sprintf("0x%04X 0x%04X 0x%s", a.h, a.endh, a.typ))
}
}
const (
cccNotify = 0x0001
cccIndicate = 0x0002
)
func newCCCD(c *ble.Characteristic) *ble.Descriptor {
d := ble.NewDescriptor(ble.ClientCharacteristicConfigUUID)
d.HandleRead(ble.ReadHandlerFunc(func(req ble.Request, rsp ble.ResponseWriter) {
cccs := req.Conn().(*conn).cccs
ccc := cccs[c.Handle]
binary.Write(rsp, binary.LittleEndian, ccc)
}))
d.HandleWrite(ble.WriteHandlerFunc(func(req ble.Request, rsp ble.ResponseWriter) {
cn := req.Conn().(*conn)
old := cn.cccs[c.Handle]
ccc := binary.LittleEndian.Uint16(req.Data())
oldNotify := old&cccNotify != 0
oldIndicate := old&cccIndicate != 0
newNotify := ccc&cccNotify != 0
newIndicate := ccc&cccIndicate != 0
if newNotify && !oldNotify {
if c.Property&ble.CharNotify == 0 {
rsp.SetStatus(ble.ErrUnlikely)
return
}
send := func(b []byte) (int, error) { return cn.svr.notify(c.ValueHandle, b) }
cn.nn[c.Handle] = ble.NewNotifier(send)
go c.NotifyHandler.ServeNotify(req, cn.nn[c.Handle])
}
if !newNotify && oldNotify {
cn.nn[c.Handle].Close()
}
if newIndicate && !oldIndicate {
if c.Property&ble.CharIndicate == 0 {
rsp.SetStatus(ble.ErrUnlikely)
return
}
send := func(b []byte) (int, error) { return cn.svr.indicate(c.ValueHandle, b) }
cn.in[c.Handle] = ble.NewNotifier(send)
go c.IndicateHandler.ServeNotify(req, cn.in[c.Handle])
}
if !newIndicate && oldIndicate {
cn.in[c.Handle].Close()
}
cn.cccs[c.Handle] = ccc
}))
return d
}