Debian Codegraph @

adv.go

raw symbols SCIP
1 package gatt
2
3 import (
4 "errors"
5 "log"
6 )
7
8 // MaxEIRPacketLength is the maximum allowed AdvertisingPacket
9 // and ScanResponsePacket length.
10 const MaxEIRPacketLength = 31
11
12 // ErrEIRPacketTooLong is the error returned when an AdvertisingPacket
13 // or ScanResponsePacket is too long.
14 var ErrEIRPacketTooLong = errors.New("max packet length is 31")
15
16 // Advertising data field types
17 const (
18 typeFlags = 0x01 // Flags
19 typeSomeUUID16 = 0x02 // Incomplete List of 16-bit Service Class UUIDs
20 typeAllUUID16 = 0x03 // Complete List of 16-bit Service Class UUIDs
21 typeSomeUUID32 = 0x04 // Incomplete List of 32-bit Service Class UUIDs
22 typeAllUUID32 = 0x05 // Complete List of 32-bit Service Class UUIDs
23 typeSomeUUID128 = 0x06 // Incomplete List of 128-bit Service Class UUIDs
24 typeAllUUID128 = 0x07 // Complete List of 128-bit Service Class UUIDs
25 typeShortName = 0x08 // Shortened Local Name
26 typeCompleteName = 0x09 // Complete Local Name
27 typeTxPower = 0x0A // Tx Power Level
28 typeClassOfDevice = 0x0D // Class of Device
29 typeSimplePairingC192 = 0x0E // Simple Pairing Hash C-192
30 typeSimplePairingR192 = 0x0F // Simple Pairing Randomizer R-192
31 typeSecManagerTK = 0x10 // Security Manager TK Value
32 typeSecManagerOOB = 0x11 // Security Manager Out of Band Flags
33 typeSlaveConnInt = 0x12 // Slave Connection Interval Range
34 typeServiceSol16 = 0x14 // List of 16-bit Service Solicitation UUIDs
35 typeServiceSol128 = 0x15 // List of 128-bit Service Solicitation UUIDs
36 typeServiceData16 = 0x16 // Service Data - 16-bit UUID
37 typePubTargetAddr = 0x17 // Public Target Address
38 typeRandTargetAddr = 0x18 // Random Target Address
39 typeAppearance = 0x19 // Appearance
40 typeAdvInterval = 0x1A // Advertising Interval
41 typeLEDeviceAddr = 0x1B // LE Bluetooth Device Address
42 typeLERole = 0x1C // LE Role
43 typeServiceSol32 = 0x1F // List of 32-bit Service Solicitation UUIDs
44 typeServiceData32 = 0x20 // Service Data - 32-bit UUID
45 typeServiceData128 = 0x21 // Service Data - 128-bit UUID
46 typeLESecConfirm = 0x22 // LE Secure Connections Confirmation Value
47 typeLESecRandom = 0x23 // LE Secure Connections Random Value
48 typeManufacturerData = 0xFF // Manufacturer Specific Data
49 )
50
51 // Advertising type flags
52 const (
53 flagLimitedDiscoverable = 0x01 // LE Limited Discoverable Mode
54 flagGeneralDiscoverable = 0x02 // LE General Discoverable Mode
55 flagLEOnly = 0x04 // BR/EDR Not Supported. Bit 37 of LMP Feature Mask Definitions (Page 0)
56 flagBothController = 0x08 // Simultaneous LE and BR/EDR to Same Device Capable (Controller).
57 flagBothHost = 0x10 // Simultaneous LE and BR/EDR to Same Device Capable (Host).
58 )
59
60 // FIXME: check the unmarshalling of this data structure.
61 type ServiceData struct {
62 UUID UUID
63 Data []byte
64 }
65
66 // This is borrowed from core bluetooth.
67 // Embedded/Linux folks might be interested in more details.
68 type Advertisement struct {
69 LocalName string
70 ManufacturerData []byte
71 ServiceData []ServiceData
72 Services []UUID
73 OverflowService []UUID
74 TxPowerLevel int
75 Connectable bool
76 SolicitedService []UUID
77 }
78
79 // This is only used in Linux port.
80 func (a *Advertisement) unmarshall(b []byte) error {
81
82 // Utility function for creating a list of uuids.
83 uuidList := func(u []UUID, d []byte, w int) []UUID {
84 for len(d) > 0 {
85 u = append(u, UUID{d[:w]})
86 d = d[w:]
87 }
88 return u
89 }
90
91 for len(b) > 0 {
92 if len(b) < 2 {
93 return errors.New("invalid advertise data")
94 }
95 l, t := b[0], b[1]
96 if len(b) < int(1+l) {
97 return errors.New("invalid advertise data")
98 }
99 d := b[2 : 1+l]
100 switch t {
101 case typeFlags:
102 // TODO: should we do anything about the discoverability here?
103 case typeSomeUUID16:
104 a.Services = uuidList(a.Services, d, 2)
105 case typeAllUUID16:
106 a.Services = uuidList(a.Services, d, 2)
107 case typeSomeUUID32:
108 a.Services = uuidList(a.Services, d, 4)
109 case typeAllUUID32:
110 a.Services = uuidList(a.Services, d, 4)
111 case typeSomeUUID128:
112 a.Services = uuidList(a.Services, d, 16)
113 case typeAllUUID128:
114 a.Services = uuidList(a.Services, d, 16)
115 case typeShortName:
116 a.LocalName = string(d)
117 case typeCompleteName:
118 a.LocalName = string(d)
119 case typeTxPower:
120 a.TxPowerLevel = int(d[0])
121 case typeServiceSol16:
122 a.SolicitedService = uuidList(a.SolicitedService, d, 2)
123 case typeServiceSol128:
124 a.SolicitedService = uuidList(a.SolicitedService, d, 16)
125 case typeServiceSol32:
126 a.SolicitedService = uuidList(a.SolicitedService, d, 4)
127 case typeManufacturerData:
128 a.ManufacturerData = make([]byte, len(d))
129 copy(a.ManufacturerData, d)
130 // case typeServiceData16,
131 // case typeServiceData32,
132 // case typeServiceData128:
133 default:
134 log.Printf("DATA: [ % X ]", d)
135 }
136 b = b[1+l:]
137 }
138 return nil
139 }
140
141 // AdvPacket is an utility to help crafting advertisment or scan response data.
142 type AdvPacket struct {
143 b []byte
144 }
145
146 // Bytes returns an 31-byte array, which contains up to 31 bytes of the packet.
147 func (a *AdvPacket) Bytes() [31]byte {
148 b := [31]byte{}
149 copy(b[:], a.b)
150 return b
151 }
152
153 // Len returns the length of the packets with a maximum of 31.
154 func (a *AdvPacket) Len() int {
155 if len(a.b) > 31 {
156 return 31
157 }
158 return len(a.b)
159 }
160
161 // AppendField appends a BLE advertising packet field.
162 // TODO: refuse to append field if it'd make the packet too long.
163 func (a *AdvPacket) AppendField(typ byte, b []byte) *AdvPacket {
164 // A field consists of len, typ, b.
165 // Len is 1 byte for typ plus len(b).
166 if len(a.b)+2+len(b) > MaxEIRPacketLength {
167 b = b[:MaxEIRPacketLength-len(a.b)-2]
168 }
169 a.b = append(a.b, byte(len(b)+1))
170 a.b = append(a.b, typ)
171 a.b = append(a.b, b...)
172 return a
173 }
174
175 // AppendFlags appends a flag field to the packet.
176 func (a *AdvPacket) AppendFlags(f byte) *AdvPacket {
177 return a.AppendField(typeFlags, []byte{f})
178 }
179
180 // AppendFlags appends a name field to the packet.
181 // If the name fits in the space, it will be append as a complete name field, otherwise a short name field.
182 func (a *AdvPacket) AppendName(n string) *AdvPacket {
183 typ := byte(typeCompleteName)
184 if len(a.b)+2+len(n) > MaxEIRPacketLength {
185 typ = byte(typeShortName)
186 }
187 return a.AppendField(typ, []byte(n))
188 }
189
190 // AppendManufacturerData appends a manufacturer data field to the packet.
191 func (a *AdvPacket) AppendManufacturerData(id uint16, b []byte) *AdvPacket {
192 d := append([]byte{uint8(id), uint8(id >> 8)}, b...)
193 return a.AppendField(typeManufacturerData, d)
194 }
195
196 // AppendUUIDFit appends a BLE advertised service UUID
197 // packet field if it fits in the packet, and reports whether the UUID fit.
198 func (a *AdvPacket) AppendUUIDFit(uu []UUID) bool {
199 // Iterate all UUIDs to see if they fit in the packet or not.
200 fit, l := true, len(a.b)
201 for _, u := range uu {
202 if u.Equal(attrGAPUUID) || u.Equal(attrGATTUUID) {
203 continue
204 }
205 l += 2 + u.Len()
206 if l > MaxEIRPacketLength {
207 fit = false
208 break
209 }
210 }
211
212 // Append the UUIDs until they no longer fit.
213 for _, u := range uu {
214 if u.Equal(attrGAPUUID) || u.Equal(attrGATTUUID) {
215 continue
216 }
217 if len(a.b)+2+u.Len() > MaxEIRPacketLength {
218 break
219 }
220 switch l = u.Len(); {
221 case l == 2 && fit:
222 a.AppendField(typeAllUUID16, u.b)
223 case l == 16 && fit:
224 a.AppendField(typeAllUUID128, u.b)
225 case l == 2 && !fit:
226 a.AppendField(typeSomeUUID16, u.b)
227 case l == 16 && !fit:
228 a.AppendField(typeSomeUUID128, u.b)
229 }
230 }
231 return fit
232 }