Debian Codegraph @

attr.go

raw symbols SCIP
1 package gatt
2
3 import "log"
4
5 // attr is a BLE attribute. It is not exported;
6 // managing attributes is an implementation detail.
7 type attr struct {
8 h uint16 // attribute handle
9 typ UUID // attribute type in UUID
10 props Property // attripute property
11 secure Property // attribute secure (implementation specific usage)
12 value []byte // attribute value
13
14 pvt interface{} // point to the corresponsing Serveice/Characteristic/Descriptor
15 }
16
17 // A attrRange is a contiguous range of attributes.
18 type attrRange struct {
19 aa []attr
20 base uint16 // handle for first attr in aa
21 }
22
23 const (
24 tooSmall = -1
25 tooLarge = -2
26 )
27
28 // idx returns the index into aa corresponding to attr a.
29 // If h is too small, idx returns tooSmall (-1).
30 // If h is too large, idx returns tooLarge (-2).
31 func (r *attrRange) idx(h int) int {
32 if h < int(r.base) {
33 return tooSmall
34 }
35 if int(h) >= int(r.base)+len(r.aa) {
36 return tooLarge
37 }
38 return h - int(r.base)
39 }
40
41 // At returns attr a.
42 func (r *attrRange) At(h uint16) (a attr, ok bool) {
43 i := r.idx(int(h))
44 if i < 0 {
45 return attr{}, false
46 }
47 return r.aa[i], true
48 }
49
50 // Subrange returns attributes in range [start, end]; it may
51 // return an empty slice. Subrange does not panic for
52 // out-of-range start or end.
53 func (r *attrRange) Subrange(start, end uint16) []attr {
54 startidx := r.idx(int(start))
55 switch startidx {
56 case tooSmall:
57 startidx = 0
58 case tooLarge:
59 return []attr{}
60 }
61
62 endidx := r.idx(int(end) + 1) // [start, end] includes its upper bound!
63 switch endidx {
64 case tooSmall:
65 return []attr{}
66 case tooLarge:
67 endidx = len(r.aa)
68 }
69 return r.aa[startidx:endidx]
70 }
71
72 func dumpAttributes(aa []attr) {
73 log.Printf("Generating attribute table:")
74 log.Printf("handle\ttype\tprops\tsecure\tpvt\tvalue")
75 for _, a := range aa {
76 log.Printf("0x%04X\t0x%s\t0x%02X\t0x%02x\t%T\t[ % X ]",
77 a.h, a.typ, int(a.props), int(a.secure), a.pvt, a.value)
78 }
79 }
80
81 func generateAttributes(ss []*Service, base uint16) *attrRange {
82 var aa []attr
83 h := base
84 last := len(ss) - 1
85 for i, s := range ss {
86 var a []attr
87 h, a = generateServiceAttributes(s, h, i == last)
88 aa = append(aa, a...)
89 }
90 dumpAttributes(aa)
91 return &attrRange{aa: aa, base: base}
92 }
93
94 func generateServiceAttributes(s *Service, h uint16, last bool) (uint16, []attr) {
95 s.h = h
96 // endh set later
97 a := attr{
98 h: h,
99 typ: attrPrimaryServiceUUID,
100 value: s.uuid.b,
101 props: CharRead,
102 pvt: s,
103 }
104 aa := []attr{a}
105 h++
106
107 for _, c := range s.Characteristics() {
108 var a []attr
109 h, a = generateCharAttributes(c, h)
110 aa = append(aa, a...)
111 }
112
113 s.endh = h - 1
114 if last {
115 h = 0xFFFF
116 s.endh = h
117 }
118
119 return h, aa
120 }
121
122 func generateCharAttributes(c *Characteristic, h uint16) (uint16, []attr) {
123 c.h = h
124 c.vh = h + 1
125 ca := attr{
126 h: c.h,
127 typ: attrCharacteristicUUID,
128 value: append([]byte{byte(c.props), byte(c.vh), byte((c.vh) >> 8)}, c.uuid.b...),
129 props: c.props,
130 pvt: c,
131 }
132 va := attr{
133 h: c.vh,
134 typ: c.uuid,
135 value: c.value,
136 props: c.props,
137 pvt: c,
138 }
139 h += 2
140
141 aa := []attr{ca, va}
142 for _, d := range c.descs {
143 aa = append(aa, generateDescAttributes(d, h))
144 h++
145 }
146
147 return h, aa
148 }
149
150 func generateDescAttributes(d *Descriptor, h uint16) attr {
151 d.h = h
152 a := attr{
153 h: h,
154 typ: d.uuid,
155 value: d.value,
156 props: d.props,
157 pvt: d,
158 }
159 return a
160 }