Debian Codegraph @

common.go

raw symbols SCIP
1 package gatt
2
3 // Supported statuses for GATT characteristic read/write operations.
4 // These correspond to att constants in the BLE spec
5 const (
6 StatusSuccess = 0
7 StatusInvalidOffset = 1
8 StatusUnexpectedError = 2
9 )
10
11 // A Request is the context for a request from a connected central device.
12 // TODO: Replace this with more general context, such as:
13 // http://godoc.org/golang.org/x/net/context
14 type Request struct {
15 Central Central
16 }
17
18 // A ReadRequest is a characteristic read request from a connected device.
19 type ReadRequest struct {
20 Request
21 Cap int // maximum allowed reply length
22 Offset int // request value offset
23 }
24
25 type Property int
26
27 // Characteristic property flags (spec 3.3.3.1)
28 const (
29 CharBroadcast Property = 0x01 // may be brocasted
30 CharRead Property = 0x02 // may be read
31 CharWriteNR Property = 0x04 // may be written to, with no reply
32 CharWrite Property = 0x08 // may be written to, with a reply
33 CharNotify Property = 0x10 // supports notifications
34 CharIndicate Property = 0x20 // supports Indications
35 CharSignedWrite Property = 0x40 // supports signed write
36 CharExtended Property = 0x80 // supports extended properties
37 )
38
39 func (p Property) String() (result string) {
40 if (p & CharBroadcast) != 0 {
41 result += "broadcast "
42 }
43 if (p & CharRead) != 0 {
44 result += "read "
45 }
46 if (p & CharWriteNR) != 0 {
47 result += "writeWithoutResponse "
48 }
49 if (p & CharWrite) != 0 {
50 result += "write "
51 }
52 if (p & CharNotify) != 0 {
53 result += "notify "
54 }
55 if (p & CharIndicate) != 0 {
56 result += "indicate "
57 }
58 if (p & CharSignedWrite) != 0 {
59 result += "authenticateSignedWrites "
60 }
61 if (p & CharExtended) != 0 {
62 result += "extendedProperties "
63 }
64 return
65 }
66
67 // A Service is a BLE service.
68 type Service struct {
69 uuid UUID
70 chars []*Characteristic
71
72 h uint16
73 endh uint16
74 }
75
76 // NewService creates and initialize a new Service using u as it's UUID.
77 func NewService(u UUID) *Service {
78 return &Service{uuid: u}
79 }
80
81 // AddCharacteristic adds a characteristic to a service.
82 // AddCharacteristic panics if the service already contains another
83 // characteristic with the same UUID.
84 func (s *Service) AddCharacteristic(u UUID) *Characteristic {
85 for _, c := range s.chars {
86 if c.uuid.Equal(u) {
87 panic("service already contains a characteristic with uuid " + u.String())
88 }
89 }
90 c := &Characteristic{uuid: u, svc: s}
91 s.chars = append(s.chars, c)
92 return c
93 }
94
95 // UUID returns the UUID of the service.
96 func (s *Service) UUID() UUID { return s.uuid }
97
98 // Name returns the specificatin name of the service according to its UUID.
99 // If the UUID is not assigne, Name returns an empty string.
100 func (s *Service) Name() string {
101 return knownServices[s.uuid.String()].Name
102 }
103
104 // Handle returns the Handle of the service.
105 func (s *Service) Handle() uint16 { return s.h }
106
107 // EndHandle returns the End Handle of the service.
108 func (s *Service) EndHandle() uint16 { return s.endh }
109
110 // SetHandle sets the Handle of the service.
111 func (s *Service) SetHandle(h uint16) { s.h = h }
112
113 // SetEndHandle sets the End Handle of the service.
114 func (s *Service) SetEndHandle(endh uint16) { s.endh = endh }
115
116 // SetCharacteristics sets the Characteristics of the service.
117 func (s *Service) SetCharacteristics(chars []*Characteristic) { s.chars = chars }
118
119 // Characteristic returns the contained characteristic of this service.
120 func (s *Service) Characteristics() []*Characteristic { return s.chars }
121
122 // A Characteristic is a BLE characteristic.
123 type Characteristic struct {
124 uuid UUID
125 props Property // enabled properties
126 secure Property // security enabled properties
127 svc *Service
128 cccd *Descriptor
129 descs []*Descriptor
130
131 value []byte
132
133 // All the following fields are only used in peripheral/server implementation.
134 rhandler ReadHandler
135 whandler WriteHandler
136 nhandler NotifyHandler
137
138 h uint16
139 vh uint16
140 endh uint16
141 }
142
143 // NewCharacteristic creates and returns a Characteristic.
144 func NewCharacteristic(u UUID, s *Service, props Property, h uint16, vh uint16) *Characteristic {
145 c := &Characteristic{
146 uuid: u,
147 svc: s,
148 props: props,
149 h: h,
150 vh: vh,
151 }
152
153 return c
154 }
155
156 // Handle returns the Handle of the characteristic.
157 func (c *Characteristic) Handle() uint16 { return c.h }
158
159 // VHandle returns the Value Handle of the characteristic.
160 func (c *Characteristic) VHandle() uint16 { return c.vh }
161
162 // EndHandle returns the End Handle of the characteristic.
163 func (c *Characteristic) EndHandle() uint16 { return c.endh }
164
165 // Descriptor returns the Descriptor of the characteristic.
166 func (c *Characteristic) Descriptor() *Descriptor { return c.cccd }
167
168 // SetHandle sets the Handle of the characteristic.
169 func (c *Characteristic) SetHandle(h uint16) { c.h = h }
170
171 // SetVHandle sets the Value Handle of the characteristic.
172 func (c *Characteristic) SetVHandle(vh uint16) { c.vh = vh }
173
174 // SetEndHandle sets the End Handle of the characteristic.
175 func (c *Characteristic) SetEndHandle(endh uint16) { c.endh = endh }
176
177 // SetDescriptor sets the Descriptor of the characteristic.
178 func (c *Characteristic) SetDescriptor(cccd *Descriptor) { c.cccd = cccd }
179
180 // SetDescriptors sets the list of Descriptor of the characteristic.
181 func (c *Characteristic) SetDescriptors(descs []*Descriptor) { c.descs = descs }
182
183 // UUID returns the UUID of the characteristic.
184 func (c *Characteristic) UUID() UUID {
185 return c.uuid
186 }
187
188 // Name returns the specificatin name of the characteristic.
189 // If the UUID is not assigned, Name returns empty string.
190 func (c *Characteristic) Name() string {
191 return knownCharacteristics[c.uuid.String()].Name
192 }
193
194 // Service returns the containing service of this characteristic.
195 func (c *Characteristic) Service() *Service {
196 return c.svc
197 }
198
199 // Properties returns the properties of this characteristic.
200 func (c *Characteristic) Properties() Property {
201 return c.props
202 }
203
204 // Descriptors returns the contained descriptors of this characteristic.
205 func (c *Characteristic) Descriptors() []*Descriptor {
206 return c.descs
207 }
208
209 // AddDescriptor adds a descriptor to a characteristic.
210 // AddDescriptor panics if the characteristic already contains another
211 // descriptor with the same UUID.
212 func (c *Characteristic) AddDescriptor(u UUID) *Descriptor {
213 for _, d := range c.descs {
214 if d.uuid.Equal(u) {
215 panic("service already contains a characteristic with uuid " + u.String())
216 }
217 }
218 d := &Descriptor{uuid: u, char: c}
219 c.descs = append(c.descs, d)
220 return d
221 }
222
223 // SetValue makes the characteristic support read requests, and returns a
224 // static value. SetValue must be called before the containing service is
225 // added to a server.
226 // SetValue panics if the characteristic has been configured with a ReadHandler.
227 func (c *Characteristic) SetValue(b []byte) {
228 if c.rhandler != nil {
229 panic("charactristic has been configured with a read handler")
230 }
231 c.props |= CharRead
232 // c.secure |= CharRead
233 c.value = make([]byte, len(b))
234 copy(c.value, b)
235 }
236
237 // HandleRead makes the characteristic support read requests, and routes read
238 // requests to h. HandleRead must be called before the containing service is
239 // added to a server.
240 // HandleRead panics if the characteristic has been configured with a static value.
241 func (c *Characteristic) HandleRead(h ReadHandler) {
242 if c.value != nil {
243 panic("charactristic has been configured with a static value")
244 }
245 c.props |= CharRead
246 // c.secure |= CharRead
247 c.rhandler = h
248 }
249
250 // HandleReadFunc calls HandleRead(ReadHandlerFunc(f)).
251 func (c *Characteristic) HandleReadFunc(f func(rsp ResponseWriter, req *ReadRequest)) {
252 c.HandleRead(ReadHandlerFunc(f))
253 }
254
255 // HandleWrite makes the characteristic support write and write-no-response
256 // requests, and routes write requests to h.
257 // The WriteHandler does not differentiate between write and write-no-response
258 // requests; it is handled automatically.
259 // HandleWrite must be called before the containing service is added to a server.
260 func (c *Characteristic) HandleWrite(h WriteHandler) {
261 c.props |= CharWrite | CharWriteNR
262 // c.secure |= CharWrite | CharWriteNR
263 c.whandler = h
264 }
265
266 // HandleWriteFunc calls HandleWrite(WriteHandlerFunc(f)).
267 func (c *Characteristic) HandleWriteFunc(f func(r Request, data []byte) (status byte)) {
268 c.HandleWrite(WriteHandlerFunc(f))
269 }
270
271 // HandleNotify makes the characteristic support notify requests, and routes
272 // notification requests to h. HandleNotify must be called before the
273 // containing service is added to a server.
274 func (c *Characteristic) HandleNotify(h NotifyHandler) {
275 if c.cccd != nil {
276 return
277 }
278 p := CharNotify | CharIndicate
279 c.props |= p
280 c.nhandler = h
281
282 // add ccc (client characteristic configuration) descriptor
283 secure := Property(0)
284 // If the characteristic requested secure notifications,
285 // then set ccc security to r/w.
286 if c.secure&p != 0 {
287 secure = CharRead | CharWrite
288 }
289 cd := &Descriptor{
290 uuid: attrClientCharacteristicConfigUUID,
291 props: CharRead | CharWrite | CharWriteNR,
292 secure: secure,
293 // FIXME: currently, we always return 0, which is inaccurate.
294 // Each connection should have it's own copy of this value.
295 value: []byte{0x00, 0x00},
296 char: c,
297 }
298 c.cccd = cd
299 c.descs = append(c.descs, cd)
300 }
301
302 // HandleNotifyFunc calls HandleNotify(NotifyHandlerFunc(f)).
303 func (c *Characteristic) HandleNotifyFunc(f func(r Request, n Notifier)) {
304 c.HandleNotify(NotifyHandlerFunc(f))
305 }
306
307 // TODO
308 // func (c *Characteristic) SubscribedCentrals() []Central{
309 // }
310
311 // Descriptor is a BLE descriptor
312 type Descriptor struct {
313 uuid UUID
314 char *Characteristic
315 props Property // enabled properties
316 secure Property // security enabled properties
317
318 h uint16
319 value []byte
320
321 rhandler ReadHandler
322 whandler WriteHandler
323 }
324
325 // Handle returns the Handle of the descriptor.
326 func (d *Descriptor) Handle() uint16 { return d.h }
327
328 // SetHandle sets the Handle of the descriptor.
329 func (d *Descriptor) SetHandle(h uint16) { d.h = h }
330
331 // NewDescriptor creates and returns a Descriptor.
332 func NewDescriptor(u UUID, h uint16, char *Characteristic) *Descriptor {
333 cd := &Descriptor{
334 uuid: u,
335 h: h,
336 char: char,
337 }
338 return cd
339 }
340
341 // UUID returns the UUID of the descriptor.
342 func (d *Descriptor) UUID() UUID {
343 return d.uuid
344 }
345
346 // Name returns the specificatin name of the descriptor.
347 // If the UUID is not assigned, returns an empty string.
348 func (d *Descriptor) Name() string {
349 return knownDescriptors[d.uuid.String()].Name
350 }
351
352 // Characteristic returns the containing characteristic of the descriptor.
353 func (d *Descriptor) Characteristic() *Characteristic {
354 return d.char
355 }
356
357 // SetValue makes the descriptor support read requests, and returns a static value.
358 // SetValue must be called before the containing service is added to a server.
359 // SetValue panics if the descriptor has already configured with a ReadHandler.
360 func (d *Descriptor) SetValue(b []byte) {
361 if d.rhandler != nil {
362 panic("descriptor has been configured with a read handler")
363 }
364 d.props |= CharRead
365 // d.secure |= CharRead
366 d.value = make([]byte, len(b))
367 copy(d.value, b)
368 }
369
370 // HandleRead makes the descriptor support read requests, and routes read requests to h.
371 // HandleRead must be called before the containing service is added to a server.
372 // HandleRead panics if the descriptor has been configured with a static value.
373 func (d *Descriptor) HandleRead(h ReadHandler) {
374 if d.value != nil {
375 panic("descriptor has been configured with a static value")
376 }
377 d.props |= CharRead
378 // d.secure |= CharRead
379 d.rhandler = h
380 }
381
382 // HandleReadFunc calls HandleRead(ReadHandlerFunc(f)).
383 func (d *Descriptor) HandleReadFunc(f func(rsp ResponseWriter, req *ReadRequest)) {
384 d.HandleRead(ReadHandlerFunc(f))
385 }
386
387 // HandleWrite makes the descriptor support write and write-no-response requests, and routes write requests to h.
388 // The WriteHandler does not differentiate between write and write-no-response requests; it is handled automatically.
389 // HandleWrite must be called before the containing service is added to a server.
390 func (d *Descriptor) HandleWrite(h WriteHandler) {
391 d.props |= CharWrite | CharWriteNR
392 // d.secure |= CharWrite | CharWriteNR
393 d.whandler = h
394 }
395
396 // HandleWriteFunc calls HandleWrite(WriteHandlerFunc(f)).
397 func (d *Descriptor) HandleWriteFunc(f func(r Request, data []byte) (status byte)) {
398 d.HandleWrite(WriteHandlerFunc(f))
399 }