-
Notifications
You must be signed in to change notification settings - Fork 137
/
att_hci.go
1203 lines (972 loc) · 26.3 KB
/
att_hci.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:build hci || ninafw || cyw43439
package bluetooth
import (
"encoding/binary"
"encoding/hex"
"errors"
"slices"
"sync"
"time"
)
const (
attOpError = 0x01
attOpMTUReq = 0x02
attOpMTUResponse = 0x03
attOpFindInfoReq = 0x04
attOpFindInfoResponse = 0x05
attOpFindByTypeReq = 0x06
attOpFindByTypeResponse = 0x07
attOpReadByTypeReq = 0x08
attOpReadByTypeResponse = 0x09
attOpReadReq = 0x0a
attOpReadResponse = 0x0b
attOpReadBlobReq = 0x0c
attOpReadBlobResponse = 0x0d
attOpReadMultiReq = 0x0e
attOpReadMultiResponse = 0x0f
attOpReadByGroupReq = 0x10
attOpReadByGroupResponse = 0x11
attOpWriteReq = 0x12
attOpWriteResponse = 0x13
attOpWriteCmd = 0x52
attOpPrepWriteReq = 0x16
attOpPrepWriteResponse = 0x17
attOpExecWriteReq = 0x18
attOpExecWriteResponse = 0x19
attOpHandleNotify = 0x1b
attOpHandleInd = 0x1d
attOpHandleCNF = 0x1e
attOpSignedWriteCmd = 0xd2
attErrorInvalidHandle = 0x01
attErrorReadNotPermitted = 0x02
attErrorWriteNotPermitted = 0x03
attErrorInvalidPDU = 0x04
attErrorAuthentication = 0x05
attErrorRequestNotSupported = 0x06
attErrorInvalidOffset = 0x07
attErrorAuthorization = 0x08
attErrorPreQueueFull = 0x09
attErrorAttrNotFound = 0x0a
attErrorAttrNotLong = 0x0b
attErrorInsuffEncrKeySize = 0x0c
attErrorInvalidAttrValueLength = 0x0d
attErrorUnlikely = 0x0e
attErrorInsuffEnc = 0x0f
attErrorUnsupportedGroupType = 0x10
attErrorInsufficientResources = 0x11
gattUnknownUUID = 0x0000
gattServiceUUID = 0x2800
gattCharacteristicUUID = 0x2803
gattDescriptorUUID = 0x2900
gattClientCharacteristicConfigUUID = 0x2902
)
var (
ErrATTTimeout = errors.New("bluetooth: ATT timeout")
ErrATTUnknownEvent = errors.New("bluetooth: ATT unknown event")
ErrATTUnknown = errors.New("bluetooth: ATT unknown error")
ErrATTOp = errors.New("bluetooth: ATT OP error")
ErrATTUnknownConnection = errors.New("bluetooth: ATT unknown connection")
)
const defaultTimeoutSeconds = 10
type rawService struct {
startHandle uint16
endHandle uint16
uuid UUID
}
func (s *rawService) Write(buf []byte) (int, error) {
s.startHandle = binary.LittleEndian.Uint16(buf[0:])
s.endHandle = binary.LittleEndian.Uint16(buf[2:])
sz := 4
switch len(buf) - 4 {
case 2:
s.uuid = New16BitUUID(binary.LittleEndian.Uint16(buf[4:]))
sz += 2
case 16:
var uuid [16]byte
copy(uuid[:], buf[4:])
slices.Reverse(uuid[:])
s.uuid = NewUUID(uuid)
sz += 16
}
return sz, nil
}
func (s *rawService) Read(p []byte) (int, error) {
binary.LittleEndian.PutUint16(p[0:], s.startHandle)
binary.LittleEndian.PutUint16(p[2:], s.endHandle)
sz := 4
switch {
case s.uuid.Is16Bit():
binary.LittleEndian.PutUint16(p[4:], s.uuid.Get16Bit())
sz += 2
default:
uuid := s.uuid.Bytes()
copy(p[4:], uuid[:])
sz += 16
}
return sz, nil
}
type rawCharacteristic struct {
startHandle uint16
properties uint8
valueHandle uint16
uuid UUID
chr *Characteristic
}
func (c *rawCharacteristic) Write(buf []byte) (int, error) {
c.startHandle = binary.LittleEndian.Uint16(buf[0:])
c.properties = buf[2]
c.valueHandle = binary.LittleEndian.Uint16(buf[3:])
sz := 5
switch len(buf) - 5 {
case 2:
c.uuid = New16BitUUID(binary.LittleEndian.Uint16(buf[5:]))
sz += 2
case 16:
var uuid [16]byte
copy(uuid[:], buf[5:])
slices.Reverse(uuid[:])
c.uuid = NewUUID(uuid)
sz += 16
}
return sz, nil
}
func (c *rawCharacteristic) Read(p []byte) (int, error) {
binary.LittleEndian.PutUint16(p[0:], c.startHandle)
p[2] = c.properties
binary.LittleEndian.PutUint16(p[3:], c.valueHandle)
sz := 5
switch {
case c.uuid.Is16Bit():
binary.LittleEndian.PutUint16(p[5:], c.uuid.Get16Bit())
sz += 2
default:
uuid := c.uuid.Bytes()
copy(p[5:], uuid[:])
sz += 16
}
return sz, nil
}
type rawDescriptor struct {
handle uint16
data []byte
}
func (d *rawDescriptor) Write(buf []byte) (int, error) {
d.handle = binary.LittleEndian.Uint16(buf[0:])
d.data = append(d.data, buf[2:]...)
return len(d.data) + 2, nil
}
func (d *rawDescriptor) Read(p []byte) (int, error) {
binary.LittleEndian.PutUint16(p[0:], d.handle)
copy(p[2:], d.data)
return len(d.data) + 2, nil
}
type rawNotification struct {
connectionHandle uint16
handle uint16
data []byte
}
type attributeType int
const (
attributeTypeService attributeType = iota
attributeTypeCharacteristic
attributeTypeCharacteristicValue
attributeTypeDescriptor
)
type rawAttribute struct {
typ attributeType
parent uint16
handle uint16
uuid UUID
permissions CharacteristicPermissions
value []byte
}
func (a *rawAttribute) Write(buf []byte) (int, error) {
return 0, errNotYetImplemented
}
func (a *rawAttribute) Read(p []byte) (int, error) {
binary.LittleEndian.PutUint16(p[0:], a.handle)
sz := 2
switch a.typ {
case attributeTypeCharacteristicValue, attributeTypeDescriptor:
switch {
case a.uuid.Is16Bit():
binary.LittleEndian.PutUint16(p[sz:], a.uuid.Get16Bit())
sz += 2
default:
uuid := a.uuid.Bytes()
copy(p[sz:], uuid[:])
sz += 16
}
default:
copy(p[sz:], a.value)
sz += len(a.value)
}
return sz, nil
}
func (a *rawAttribute) length() int {
switch a.typ {
case attributeTypeCharacteristicValue, attributeTypeDescriptor:
switch {
case a.uuid.Is16Bit():
return 2
default:
return 16
}
default:
return len(a.value)
}
}
type connectData struct {
responded bool
errored bool
lastErrorOpcode uint8
lastErrorHandle uint16
lastErrorCode uint8
mtu uint16
maxMTU uint16
services []rawService
characteristics []rawCharacteristic
descriptors []rawDescriptor
value []byte
}
type att struct {
hci *hci
busy sync.Mutex
mtu uint16
maxMTU uint16
notifications chan rawNotification
connections []uint16
connectionsData map[uint16]*connectData
lastHandle uint16
localServices []rawService
localCharacteristics []rawCharacteristic
attributes []rawAttribute
}
func newATT(hci *hci) *att {
return &att{
hci: hci,
localCharacteristics: []rawCharacteristic{},
notifications: make(chan rawNotification, 32),
connections: []uint16{},
connectionsData: make(map[uint16]*connectData),
lastHandle: 0x0001,
attributes: []rawAttribute{},
localServices: []rawService{},
maxMTU: 248,
}
}
func (a *att) readByGroupReq(connectionHandle, startHandle, endHandle uint16, uuid shortUUID) error {
if debug {
println("att.readByGroupReq:", connectionHandle, startHandle, endHandle, uuid)
}
a.busy.Lock()
defer a.busy.Unlock()
var b [7]byte
b[0] = attOpReadByGroupReq
binary.LittleEndian.PutUint16(b[1:], startHandle)
binary.LittleEndian.PutUint16(b[3:], endHandle)
binary.LittleEndian.PutUint16(b[5:], uint16(uuid))
if err := a.sendReq(connectionHandle, b[:]); err != nil {
return err
}
return a.waitUntilResponse(connectionHandle)
}
func (a *att) readByTypeReq(connectionHandle, startHandle, endHandle uint16, typ uint16) error {
if debug {
println("att.readByTypeReq:", connectionHandle, startHandle, endHandle, typ)
}
a.busy.Lock()
defer a.busy.Unlock()
var b [7]byte
b[0] = attOpReadByTypeReq
binary.LittleEndian.PutUint16(b[1:], startHandle)
binary.LittleEndian.PutUint16(b[3:], endHandle)
binary.LittleEndian.PutUint16(b[5:], typ)
if err := a.sendReq(connectionHandle, b[:]); err != nil {
return err
}
return a.waitUntilResponse(connectionHandle)
}
func (a *att) findInfoReq(connectionHandle, startHandle, endHandle uint16) error {
if debug {
println("att.findInfoReq:", connectionHandle, startHandle, endHandle)
}
a.busy.Lock()
defer a.busy.Unlock()
var b [5]byte
b[0] = attOpFindInfoReq
binary.LittleEndian.PutUint16(b[1:], startHandle)
binary.LittleEndian.PutUint16(b[3:], endHandle)
if err := a.sendReq(connectionHandle, b[:]); err != nil {
return err
}
return a.waitUntilResponse(connectionHandle)
}
func (a *att) readReq(connectionHandle, valueHandle uint16) error {
if debug {
println("att.readReq:", connectionHandle, valueHandle)
}
a.busy.Lock()
defer a.busy.Unlock()
var b [3]byte
b[0] = attOpReadReq
binary.LittleEndian.PutUint16(b[1:], valueHandle)
if err := a.sendReq(connectionHandle, b[:]); err != nil {
return err
}
return a.waitUntilResponse(connectionHandle)
}
func (a *att) writeCmd(connectionHandle, valueHandle uint16, data []byte) error {
if debug {
println("att.writeCmd:", connectionHandle, valueHandle, hex.EncodeToString(data))
}
a.busy.Lock()
defer a.busy.Unlock()
var b [3]byte
b[0] = attOpWriteCmd
binary.LittleEndian.PutUint16(b[1:], valueHandle)
if err := a.sendReq(connectionHandle, append(b[:], data...)); err != nil {
return err
}
return nil
}
func (a *att) writeReq(connectionHandle, valueHandle uint16, data []byte) error {
if debug {
println("att.writeReq:", connectionHandle, valueHandle, hex.EncodeToString(data))
}
a.busy.Lock()
defer a.busy.Unlock()
var b [3]byte
b[0] = attOpWriteReq
binary.LittleEndian.PutUint16(b[1:], valueHandle)
if err := a.sendReq(connectionHandle, append(b[:], data...)); err != nil {
return err
}
return a.waitUntilResponse(connectionHandle)
}
func (a *att) mtuReq(connectionHandle uint16) error {
if debug {
println("att.mtuReq:", connectionHandle)
}
cd, err := a.findConnectionData(connectionHandle)
if err != nil {
return err
}
a.busy.Lock()
defer a.busy.Unlock()
var b [3]byte
b[0] = attOpMTUReq
binary.LittleEndian.PutUint16(b[1:], cd.mtu)
if err := a.sendReq(connectionHandle, b[:]); err != nil {
return err
}
return a.waitUntilResponse(connectionHandle)
}
func (a *att) setMaxMTU(mtu uint16) error {
a.maxMTU = mtu
return nil
}
func (a *att) sendReq(handle uint16, data []byte) error {
if err := a.clearResponse(handle); err != nil {
return err
}
if debug {
println("att.sendReq:", handle, "data:", hex.EncodeToString(data))
}
if err := a.hci.sendAclPkt(handle, attCID, data); err != nil {
return err
}
return nil
}
func (a *att) sendNotification(handle uint16, data []byte) error {
if debug {
println("att.sendNotifications:", handle, "data:", hex.EncodeToString(data))
}
a.busy.Lock()
defer a.busy.Unlock()
var b [3]byte
b[0] = attOpHandleNotify
binary.LittleEndian.PutUint16(b[1:], handle)
for connection := range a.connections {
if debug {
println("att.sendNotifications: sending to", connection)
}
if err := a.hci.sendAclPkt(uint16(connection), attCID, append(b[:], data...)); err != nil {
return err
}
}
return nil
}
func (a *att) sendError(handle uint16, opcode uint8, hdl uint16, code uint8) error {
if err := a.clearResponse(handle); err != nil {
return err
}
if debug {
println("att.sendError:", handle, "data:", opcode, hdl, code)
}
var b [5]byte
b[0] = attOpError
b[1] = opcode
binary.LittleEndian.PutUint16(b[2:], hdl)
b[4] = code
if err := a.hci.sendAclPkt(handle, attCID, b[:]); err != nil {
return err
}
return nil
}
func (a *att) handleData(handle uint16, buf []byte) error {
if debug {
println("att.handleData:", handle, "data:", hex.EncodeToString(buf))
}
cd, err := a.findConnectionData(handle)
if err != nil {
return err
}
switch buf[0] {
case attOpError:
cd.errored = true
cd.lastErrorOpcode = buf[1]
cd.lastErrorHandle = binary.LittleEndian.Uint16(buf[2:])
cd.lastErrorCode = buf[4]
if debug {
println("att.handleData: attOpERROR", handle, cd.lastErrorOpcode, cd.lastErrorCode)
}
return ErrATTOp
case attOpMTUReq:
if debug {
println("att.handleData: attOpMTUReq", hex.EncodeToString(buf))
}
mtu := binary.LittleEndian.Uint16(buf[1:])
if mtu > a.maxMTU {
mtu = a.maxMTU
}
// save mtu for connection
cd.mtu = mtu
var b [3]byte
b[0] = attOpMTUResponse
binary.LittleEndian.PutUint16(b[1:], mtu)
if err := a.hci.sendAclPkt(handle, attCID, b[:]); err != nil {
return err
}
case attOpMTUResponse:
if debug {
println("att.handleData: attOpMTUResponse")
}
cd.responded = true
cd.mtu = binary.LittleEndian.Uint16(buf[1:])
case attOpFindInfoReq:
if debug {
println("att.handleData: attOpFindInfoReq")
}
startHandle := binary.LittleEndian.Uint16(buf[1:])
endHandle := binary.LittleEndian.Uint16(buf[3:])
return a.handleFindInfoReq(handle, startHandle, endHandle)
case attOpFindInfoResponse:
if debug {
println("att.handleData: attOpFindInfoResponse")
}
cd.responded = true
lengthPerDescriptor := int(buf[1])
for i := 2; i < len(buf); i += lengthPerDescriptor {
d := rawDescriptor{}
d.Write(buf[i : i+lengthPerDescriptor])
if debug {
println("att.handleData: descriptor", d.handle, hex.EncodeToString(d.data))
}
cd.descriptors = append(cd.descriptors, d)
}
case attOpFindByTypeReq:
if debug {
println("att.handleData: attOpFindByTypeReq")
}
case attOpReadByTypeReq:
if debug {
println("att.handleData: attOpReadByTypeReq")
}
startHandle := binary.LittleEndian.Uint16(buf[1:])
endHandle := binary.LittleEndian.Uint16(buf[3:])
uuid := shortUUID(binary.LittleEndian.Uint16(buf[5:]))
return a.handleReadByTypeReq(handle, startHandle, endHandle, uuid)
case attOpReadByTypeResponse:
if debug {
println("att.handleData: attOpReadByTypeResponse")
}
cd.responded = true
lengthPerCharacteristic := int(buf[1])
for i := 2; i < len(buf); i += lengthPerCharacteristic {
c := rawCharacteristic{}
c.Write(buf[i : i+lengthPerCharacteristic])
if debug {
println("att.handleData: characteristic", c.startHandle, c.properties, c.valueHandle, c.uuid.String())
}
cd.characteristics = append(cd.characteristics, c)
}
return nil
case attOpReadByGroupReq:
if debug {
println("att.handleData: attOpReadByGroupReq")
}
startHandle := binary.LittleEndian.Uint16(buf[1:])
endHandle := binary.LittleEndian.Uint16(buf[3:])
uuid := shortUUID(binary.LittleEndian.Uint16(buf[5:]))
return a.handleReadByGroupReq(handle, startHandle, endHandle, uuid)
case attOpReadByGroupResponse:
if debug {
println("att.handleData: attOpReadByGroupResponse")
}
cd.responded = true
lengthPerService := int(buf[1])
for i := 2; i < len(buf); i += lengthPerService {
service := rawService{}
service.Write(buf[i : i+lengthPerService])
if debug {
println("att.handleData: service", service.startHandle, service.endHandle, service.uuid.String())
}
cd.services = append(cd.services, service)
}
return nil
case attOpReadReq:
if debug {
println("att.handleData: attOpReadReq")
}
attrHandle := binary.LittleEndian.Uint16(buf[1:])
return a.handleReadReq(handle, attrHandle)
case attOpReadBlobReq:
if debug {
println("att.handleData: attOpReadBlobReq")
}
case attOpReadResponse:
if debug {
println("att.handleData: attOpReadResponse")
}
cd.responded = true
cd.value = append(cd.value, buf[1:]...)
case attOpWriteReq:
if debug {
println("att.handleData: attOpWriteReq")
}
attrHandle := binary.LittleEndian.Uint16(buf[1:])
return a.handleWriteReq(handle, attrHandle, buf[3:])
case attOpWriteCmd:
if debug {
println("att.handleData: attOpWriteCmd")
}
case attOpWriteResponse:
if debug {
println("att.handleData: attOpWriteResponse")
}
cd.responded = true
case attOpPrepWriteReq:
if debug {
println("att.handleData: attOpPrepWriteReq")
}
case attOpExecWriteReq:
if debug {
println("att.handleData: attOpExecWriteReq")
}
case attOpHandleNotify:
if debug {
println("att.handleData: attOpHandleNotify")
}
not := rawNotification{
connectionHandle: handle,
handle: binary.LittleEndian.Uint16(buf[1:]),
data: []byte{},
}
not.data = append(not.data, buf[3:]...)
select {
case a.notifications <- not:
default:
// out of space, drop notification :(
}
case attOpHandleInd:
if debug {
println("att.handleData: attOpHandleInd")
}
case attOpHandleCNF:
if debug {
println("att.handleData: attOpHandleCNF")
}
case attOpReadMultiReq:
if debug {
println("att.handleData: attOpReadMultiReq")
}
case attOpSignedWriteCmd:
if debug {
println("att.handleData: attOpSignedWriteCmd")
}
default:
if debug {
println("att.handleData: unknown")
}
}
return nil
}
func (a *att) handleReadByGroupReq(handle, start, end uint16, uuid shortUUID) error {
var response [64]byte
response[0] = attOpReadByGroupResponse
response[1] = 0x0 // length per service
pos := 2
switch uuid {
case shortUUID(gattServiceUUID):
for _, s := range a.localServices {
if s.startHandle >= start && s.endHandle <= end {
if debug {
println("attOpReadByGroupReq: replying with service", s.startHandle, s.endHandle, s.uuid.String())
}
length := 20
if s.uuid.Is16Bit() {
length = 6
}
if response[1] == 0 {
response[1] = byte(length)
} else if response[1] != byte(length) {
// change of UUID size
break
}
s.Read(response[pos : pos+length])
pos += length
if uint16(pos+length) > a.mtu {
break
}
}
}
switch {
case pos > 2:
if err := a.hci.sendAclPkt(handle, attCID, response[:pos]); err != nil {
return err
}
default:
if err := a.sendError(handle, attOpReadByGroupReq, start, attErrorAttrNotFound); err != nil {
return err
}
}
return nil
default:
if debug {
println("handleReadByGroupReq: unknown uuid", New16BitUUID(uint16(uuid)).String())
}
if err := a.sendError(handle, attOpReadByGroupReq, start, attErrorAttrNotFound); err != nil {
return err
}
return nil
}
}
func (a *att) handleReadByTypeReq(handle, start, end uint16, uuid shortUUID) error {
var response [64]byte
response[0] = attOpReadByTypeResponse
pos := 0
switch uuid {
case shortUUID(gattCharacteristicUUID):
pos = 2
response[1] = 0
for _, c := range a.localCharacteristics {
if debug {
println("handleReadByTypeReq: looking at characteristic", c.startHandle, c.uuid.String())
}
if c.startHandle >= start && c.valueHandle <= end {
if debug {
println("handleReadByTypeReq: replying with characteristic", c.startHandle, c.uuid.String())
}
length := 21
if c.uuid.Is16Bit() {
length = 7
}
if response[1] == 0 {
response[1] = byte(length)
} else if response[1] != byte(length) {
// change of UUID size
break
}
c.Read(response[pos : pos+length])
pos += length
if uint16(pos+length) > a.mtu {
break
}
}
}
switch {
case pos > 2:
if err := a.hci.sendAclPkt(handle, attCID, response[:pos]); err != nil {
return err
}
default:
if err := a.sendError(handle, attOpReadByTypeReq, start, attErrorAttrNotFound); err != nil {
return err
}
}
return nil
default:
if debug {
println("handleReadByTypeReq: unknown uuid", New16BitUUID(uint16(uuid)).String())
}
if err := a.sendError(handle, attOpReadByTypeReq, start, attErrorAttrNotFound); err != nil {
return err
}
return nil
}
}
func (a *att) handleFindInfoReq(handle, start, end uint16) error {
var response [64]byte
response[0] = attOpFindInfoResponse
pos := 0
pos = 2
infoType := 0
response[1] = 0
for _, attr := range a.attributes {
if debug {
println("handleFindInfoReq: looking at attribute")
}
if attr.handle >= start && attr.handle <= end {
if debug {
println("handleFindInfoReq: replying with attribute", attr.handle, attr.uuid.String(), attr.typ)
}
if attr.typ == attributeTypeCharacteristicValue || attr.typ == attributeTypeDescriptor {
infoType = 1
} else {
infoType = 2
}
length := attr.length() + 2
if response[1] == 0 {
response[1] = byte(infoType)
} else if response[1] != byte(infoType) {
// change of info type
break
}
attr.Read(response[pos : pos+length])
pos += length
if uint16(pos+length) >= a.mtu {
break
}
}
}
switch {
case pos > 2:
if err := a.hci.sendAclPkt(handle, attCID, response[:pos]); err != nil {
return err
}
default:
if err := a.sendError(handle, attOpFindInfoReq, start, attErrorAttrNotFound); err != nil {
return err
}
}
return nil
}
func (a *att) handleReadReq(handle, attrHandle uint16) error {
attr := a.findAttribute(attrHandle)
if attr == nil {
if debug {
println("att.handleReadReq: attribute not found", attrHandle)
}
return a.sendError(handle, attOpReadReq, attrHandle, attErrorAttrNotFound)
}
var response [64]byte
response[0] = attOpReadResponse
pos := 1
switch attr.typ {
case attributeTypeCharacteristicValue:
if debug {
println("att.handleReadReq: reading characteristic value", attrHandle)
}
c := a.findCharacteristic(attr.parent)
if c != nil && c.chr != nil {
value, err := c.chr.readValue()
if err != nil {
return a.sendError(handle, attOpReadReq, attrHandle, attErrorReadNotPermitted)
}
copy(response[pos:], value)
pos += len(value)
if err := a.hci.sendAclPkt(handle, attCID, response[:pos]); err != nil {
return err
}
return nil
}
case attributeTypeDescriptor:
if debug {
println("att.handleReadReq: reading descriptor", attrHandle)
}
c := a.findCharacteristic(attr.parent)
if c != nil && c.chr != nil {
cccd, err := c.chr.readCCCD()
if err != nil {
return a.sendError(handle, attOpReadReq, attrHandle, attErrorReadNotPermitted)
}
binary.LittleEndian.PutUint16(response[pos:], cccd)
pos += 2
if err := a.hci.sendAclPkt(handle, attCID, response[:pos]); err != nil {
return err
}
return nil
}
}
return a.sendError(handle, attOpReadReq, attrHandle, attErrorReadNotPermitted)
}
func (a *att) handleWriteReq(handle, attrHandle uint16, data []byte) error {
attr := a.findAttribute(attrHandle)
if attr == nil {
if debug {
println("att.handleWriteReq: attribute not found", attrHandle)