File: att.py

package info (click to toggle)
python-bumble 0.0.225-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 9,464 kB
  • sloc: python: 75,258; java: 3,782; javascript: 823; xml: 203; sh: 172; makefile: 8
file content (1088 lines) | stat: -rw-r--r-- 38,553 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
# Copyright 2021-2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# -----------------------------------------------------------------------------
# ATT - Attribute Protocol
#
# See Bluetooth spec @ Vol 3, Part F
#
# -----------------------------------------------------------------------------

# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
from __future__ import annotations

import dataclasses
import enum
import functools
import inspect
import struct
from collections.abc import Awaitable, Callable, Sequence
from typing import (
    TYPE_CHECKING,
    ClassVar,
    Generic,
    TypeAlias,
    TypeVar,
)

from typing_extensions import TypeIs

from bumble import hci, l2cap, utils
from bumble.colors import color
from bumble.core import UUID, InvalidOperationError, ProtocolError
from bumble.hci import HCI_Object

# -----------------------------------------------------------------------------
# Typing
# -----------------------------------------------------------------------------
if TYPE_CHECKING:
    from bumble.device import Connection

_T = TypeVar('_T')

Bearer: TypeAlias = "Connection | l2cap.LeCreditBasedChannel"
EnhancedBearer: TypeAlias = l2cap.LeCreditBasedChannel


def is_enhanced_bearer(bearer: Bearer) -> TypeIs[EnhancedBearer]:
    return isinstance(bearer, EnhancedBearer)


# -----------------------------------------------------------------------------
# Constants
# -----------------------------------------------------------------------------
# fmt: off
# pylint: disable=line-too-long

ATT_CID = 0x04
ATT_PSM = 0x001F
EATT_PSM = 0x0027

class Opcode(hci.SpecableEnum):
    ATT_ERROR_RESPONSE                  = 0x01
    ATT_EXCHANGE_MTU_REQUEST            = 0x02
    ATT_EXCHANGE_MTU_RESPONSE           = 0x03
    ATT_FIND_INFORMATION_REQUEST        = 0x04
    ATT_FIND_INFORMATION_RESPONSE       = 0x05
    ATT_FIND_BY_TYPE_VALUE_REQUEST      = 0x06
    ATT_FIND_BY_TYPE_VALUE_RESPONSE     = 0x07
    ATT_READ_BY_TYPE_REQUEST            = 0x08
    ATT_READ_BY_TYPE_RESPONSE           = 0x09
    ATT_READ_REQUEST                    = 0x0A
    ATT_READ_RESPONSE                   = 0x0B
    ATT_READ_BLOB_REQUEST               = 0x0C
    ATT_READ_BLOB_RESPONSE              = 0x0D
    ATT_READ_MULTIPLE_REQUEST           = 0x0E
    ATT_READ_MULTIPLE_RESPONSE          = 0x0F
    ATT_READ_BY_GROUP_TYPE_REQUEST      = 0x10
    ATT_READ_BY_GROUP_TYPE_RESPONSE     = 0x11
    ATT_READ_MULTIPLE_VARIABLE_REQUEST  = 0x20
    ATT_READ_MULTIPLE_VARIABLE_RESPONSE = 0x21
    ATT_WRITE_REQUEST                   = 0x12
    ATT_WRITE_RESPONSE                  = 0x13
    ATT_WRITE_COMMAND                   = 0x52
    ATT_SIGNED_WRITE_COMMAND            = 0xD2
    ATT_PREPARE_WRITE_REQUEST           = 0x16
    ATT_PREPARE_WRITE_RESPONSE          = 0x17
    ATT_EXECUTE_WRITE_REQUEST           = 0x18
    ATT_EXECUTE_WRITE_RESPONSE          = 0x19
    ATT_HANDLE_VALUE_NOTIFICATION       = 0x1B
    ATT_HANDLE_VALUE_INDICATION         = 0x1D
    ATT_HANDLE_VALUE_CONFIRMATION       = 0x1E

ATT_REQUESTS = [
    Opcode.ATT_EXCHANGE_MTU_REQUEST,
    Opcode.ATT_FIND_INFORMATION_REQUEST,
    Opcode.ATT_FIND_BY_TYPE_VALUE_REQUEST,
    Opcode.ATT_READ_BY_TYPE_REQUEST,
    Opcode.ATT_READ_REQUEST,
    Opcode.ATT_READ_BLOB_REQUEST,
    Opcode.ATT_READ_MULTIPLE_REQUEST,
    Opcode.ATT_READ_BY_GROUP_TYPE_REQUEST,
    Opcode.ATT_READ_MULTIPLE_VARIABLE_REQUEST,
    Opcode.ATT_WRITE_REQUEST,
    Opcode.ATT_PREPARE_WRITE_REQUEST,
    Opcode.ATT_EXECUTE_WRITE_REQUEST,
]

ATT_RESPONSES = [
    Opcode.ATT_ERROR_RESPONSE,
    Opcode.ATT_EXCHANGE_MTU_RESPONSE,
    Opcode.ATT_FIND_INFORMATION_RESPONSE,
    Opcode.ATT_FIND_BY_TYPE_VALUE_RESPONSE,
    Opcode.ATT_READ_BY_TYPE_RESPONSE,
    Opcode.ATT_READ_RESPONSE,
    Opcode.ATT_READ_BLOB_RESPONSE,
    Opcode.ATT_READ_MULTIPLE_RESPONSE,
    Opcode.ATT_READ_BY_GROUP_TYPE_RESPONSE,
    Opcode.ATT_READ_MULTIPLE_VARIABLE_RESPONSE,
    Opcode.ATT_WRITE_RESPONSE,
    Opcode.ATT_PREPARE_WRITE_RESPONSE,
    Opcode.ATT_EXECUTE_WRITE_RESPONSE,
]

class ErrorCode(hci.SpecableEnum):
    '''
    See

    * Bluetooth spec @ Vol 3, Part F - 3.4.1.1 Error Response
    * Core Specification Supplement: Common Profile And Service Error Codes
    '''
    INVALID_HANDLE                   = 0x01
    READ_NOT_PERMITTED               = 0x02
    WRITE_NOT_PERMITTED              = 0x03
    INVALID_PDU                      = 0x04
    INSUFFICIENT_AUTHENTICATION      = 0x05
    REQUEST_NOT_SUPPORTED            = 0x06
    INVALID_OFFSET                   = 0x07
    INSUFFICIENT_AUTHORIZATION       = 0x08
    PREPARE_QUEUE_FULL               = 0x09
    ATTRIBUTE_NOT_FOUND              = 0x0A
    ATTRIBUTE_NOT_LONG               = 0x0B
    INSUFFICIENT_ENCRYPTION_KEY_SIZE = 0x0C
    INVALID_ATTRIBUTE_LENGTH         = 0x0D
    UNLIKELY_ERROR                   = 0x0E
    INSUFFICIENT_ENCRYPTION          = 0x0F
    UNSUPPORTED_GROUP_TYPE           = 0x10
    INSUFFICIENT_RESOURCES           = 0x11
    DATABASE_OUT_OF_SYNC             = 0x12
    VALUE_NOT_ALLOWED                = 0x13
    # 0x80 – 0x9F: Application Error
    # 0xE0 – 0xFF: Common Profile and Service Error Codes
    WRITE_REQUEST_REJECTED           = 0xFC
    CCCD_IMPROPERLY_CONFIGURED       = 0xFD
    PROCEDURE_ALREADY_IN_PROGRESS    = 0xFE
    OUT_OF_RANGE                     = 0xFF

# Backward Compatible Constants
ATT_INVALID_HANDLE_ERROR                   = ErrorCode.INVALID_HANDLE
ATT_READ_NOT_PERMITTED_ERROR               = ErrorCode.READ_NOT_PERMITTED
ATT_WRITE_NOT_PERMITTED_ERROR              = ErrorCode.WRITE_NOT_PERMITTED
ATT_INVALID_PDU_ERROR                      = ErrorCode.INVALID_PDU
ATT_INSUFFICIENT_AUTHENTICATION_ERROR      = ErrorCode.INSUFFICIENT_AUTHENTICATION
ATT_REQUEST_NOT_SUPPORTED_ERROR            = ErrorCode.REQUEST_NOT_SUPPORTED
ATT_INVALID_OFFSET_ERROR                   = ErrorCode.INVALID_OFFSET
ATT_INSUFFICIENT_AUTHORIZATION_ERROR       = ErrorCode.INSUFFICIENT_AUTHORIZATION
ATT_PREPARE_QUEUE_FULL_ERROR               = ErrorCode.PREPARE_QUEUE_FULL
ATT_ATTRIBUTE_NOT_FOUND_ERROR              = ErrorCode.ATTRIBUTE_NOT_FOUND
ATT_ATTRIBUTE_NOT_LONG_ERROR               = ErrorCode.ATTRIBUTE_NOT_LONG
ATT_INSUFFICIENT_ENCRYPTION_KEY_SIZE_ERROR = ErrorCode.INSUFFICIENT_ENCRYPTION_KEY_SIZE
ATT_INVALID_ATTRIBUTE_LENGTH_ERROR         = ErrorCode.INVALID_ATTRIBUTE_LENGTH
ATT_UNLIKELY_ERROR_ERROR                   = ErrorCode.UNLIKELY_ERROR
ATT_INSUFFICIENT_ENCRYPTION_ERROR          = ErrorCode.INSUFFICIENT_ENCRYPTION
ATT_UNSUPPORTED_GROUP_TYPE_ERROR           = ErrorCode.UNSUPPORTED_GROUP_TYPE
ATT_INSUFFICIENT_RESOURCES_ERROR           = ErrorCode.INSUFFICIENT_RESOURCES

ATT_DEFAULT_MTU = 23

HANDLE_FIELD_SPEC    = {'size': 2, 'mapper': lambda x: f'0x{x:04X}'}
_SET_OF_HANDLES_METADATA = hci.metadata({
                'parser': lambda data, offset: (
                    len(data),
                    [
                        struct.unpack_from('<H', data, i)[0]
                        for i in range(offset, len(data), 2)
                    ],
                ),
                'serializer': lambda handles: b''.join(
                    [struct.pack('<H', handle) for handle in handles]
                ),
            })

# fmt: on
# pylint: enable=line-too-long
# pylint: disable=invalid-name


# -----------------------------------------------------------------------------
# Exceptions
# -----------------------------------------------------------------------------
class ATT_Error(ProtocolError):
    error_code: int
    att_handle: int

    def __init__(
        self, error_code: int, att_handle: int = 0x0000, message: str = ''
    ) -> None:
        super().__init__(
            error_code,
            error_namespace='att',
            error_name=ErrorCode(error_code).name,
        )
        self.att_handle = att_handle
        self.message = message

    def __str__(self):
        return (
            f'ATT_Error(error={self.error_name}, '
            f'handle={self.att_handle:04X}): {self.message}'
        )


# -----------------------------------------------------------------------------
# Attribute Protocol
# -----------------------------------------------------------------------------
@dataclasses.dataclass
class ATT_PDU:
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.3 ATTRIBUTE PDU
    '''

    pdu_classes: ClassVar[dict[int, type[ATT_PDU]]] = {}
    fields: ClassVar[hci.Fields] = ()
    op_code: int = dataclasses.field(init=False)
    name: str = dataclasses.field(init=False)
    _payload: bytes | None = dataclasses.field(default=None, init=False)

    @classmethod
    def from_bytes(cls, pdu: bytes) -> ATT_PDU:
        op_code = pdu[0]

        subclass = ATT_PDU.pdu_classes.get(op_code)
        if subclass is None:
            instance = ATT_PDU()
            instance.op_code = op_code
            instance.payload = pdu[1:]
            instance.name = Opcode(op_code).name
            return instance
        instance = subclass(**HCI_Object.dict_from_bytes(pdu, 1, subclass.fields))
        instance.payload = pdu[1:]
        return instance

    _PDU = TypeVar("_PDU", bound="ATT_PDU")

    @classmethod
    def subclass(cls, subclass: type[_PDU]) -> type[_PDU]:
        subclass.name = subclass.__name__.upper()
        subclass.op_code = Opcode[subclass.name]
        subclass.fields = HCI_Object.fields_from_dataclass(subclass)

        # Register a factory for this class
        ATT_PDU.pdu_classes[subclass.op_code] = subclass

        return subclass

    def init_from_bytes(self, pdu, offset):
        return HCI_Object.init_from_bytes(self, pdu, offset, self.fields)

    @property
    def is_command(self):
        return ((self.op_code >> 6) & 1) == 1

    @property
    def has_authentication_signature(self):
        return ((self.op_code >> 7) & 1) == 1

    @property
    def payload(self) -> bytes:
        if self._payload is None:
            self._payload = HCI_Object.dict_to_bytes(self.__dict__, self.fields)
        return self._payload

    @payload.setter
    def payload(self, value: bytes):
        self._payload = value

    def __bytes__(self) -> bytes:
        return bytes([self.op_code]) + self.payload

    def __str__(self):
        result = color(self.name, 'yellow')
        if fields := getattr(self, 'fields', None):
            result += ':\n' + HCI_Object.format_fields(self.__dict__, fields, '  ')
        else:
            if self.payload:
                result += f': {self.payload.hex()}'
        return result


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Error_Response(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.1.1 Error Response
    '''

    request_opcode_in_error: int = dataclasses.field(metadata=Opcode.type_metadata(1))
    attribute_handle_in_error: int = dataclasses.field(
        metadata=hci.metadata(HANDLE_FIELD_SPEC)
    )
    error_code: int = dataclasses.field(metadata=ErrorCode.type_metadata(1))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Exchange_MTU_Request(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.2.1 Exchange MTU Request
    '''

    client_rx_mtu: int = dataclasses.field(metadata=hci.metadata(2))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Exchange_MTU_Response(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.2.2 Exchange MTU Response
    '''

    server_rx_mtu: int = dataclasses.field(metadata=hci.metadata(2))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Find_Information_Request(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.3.1 Find Information Request
    '''

    starting_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    ending_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Find_Information_Response(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.3.2 Find Information Response
    '''

    format: int = dataclasses.field(metadata=hci.metadata(1))
    information_data: bytes = dataclasses.field(metadata=hci.metadata("*"))
    information: list[tuple[int, bytes]] = dataclasses.field(init=False)

    def __post_init__(self) -> None:
        self.information = []
        offset = 0
        uuid_size = 2 if self.format == 1 else 16
        while offset + uuid_size <= len(self.information_data):
            handle = struct.unpack_from('<H', self.information_data, offset)[0]
            uuid = self.information_data[2 + offset : 2 + offset + uuid_size]
            self.information.append((handle, uuid))
            offset += 2 + uuid_size

    def __str__(self):
        result = color(self.name, 'yellow')
        result += ':\n' + HCI_Object.format_fields(
            self.__dict__,
            [
                ('format', 1),
                (
                    'information',
                    {
                        'mapper': lambda x: ', '.join(
                            [f'0x{handle:04X}:{uuid.hex()}' for handle, uuid in x]
                        )
                    },
                ),
            ],
            '  ',
        )
        return result


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Find_By_Type_Value_Request(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.3.3 Find By Type Value Request
    '''

    starting_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    ending_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    attribute_type: UUID = dataclasses.field(metadata=hci.metadata(UUID.parse_uuid_2))
    attribute_value: bytes = dataclasses.field(metadata=hci.metadata("*"))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Find_By_Type_Value_Response(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.3.4 Find By Type Value Response
    '''

    handles_information_list: bytes = dataclasses.field(metadata=hci.metadata("*"))
    handles_information: list[tuple[int, int]] = dataclasses.field(init=False)

    def __post_init__(self) -> None:
        self.handles_information = []
        offset = 0
        while offset + 4 <= len(self.handles_information_list):
            found_attribute_handle, group_end_handle = struct.unpack_from(
                '<HH', self.handles_information_list, offset
            )
            self.handles_information.append((found_attribute_handle, group_end_handle))
            offset += 4

    def __str__(self):
        result = color(self.name, 'yellow')
        result += ':\n' + HCI_Object.format_fields(
            self.__dict__,
            [
                (
                    'handles_information',
                    {
                        'mapper': lambda x: ', '.join(
                            [
                                f'0x{handle1:04X}-0x{handle2:04X}'
                                for handle1, handle2 in x
                            ]
                        )
                    },
                )
            ],
            '  ',
        )
        return result


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Read_By_Type_Request(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.4.1 Read By Type Request
    '''

    starting_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    ending_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    attribute_type: UUID = dataclasses.field(metadata=hci.metadata(UUID.parse_uuid))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Read_By_Type_Response(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.4.2 Read By Type Response
    '''

    length: int = dataclasses.field(metadata=hci.metadata(1))
    attribute_data_list: bytes = dataclasses.field(metadata=hci.metadata("*"))
    attributes: list[tuple[int, bytes]] = dataclasses.field(init=False)

    def __post_init__(self) -> None:
        self.attributes = []
        offset = 0
        while self.length != 0 and offset + self.length <= len(
            self.attribute_data_list
        ):
            (attribute_handle,) = struct.unpack_from(
                '<H', self.attribute_data_list, offset
            )
            attribute_value = self.attribute_data_list[
                offset + 2 : offset + self.length
            ]
            self.attributes.append((attribute_handle, attribute_value))
            offset += self.length

    def __str__(self):
        result = color(self.name, 'yellow')
        result += ':\n' + HCI_Object.format_fields(
            self.__dict__,
            [
                ('length', 1),
                (
                    'attributes',
                    {
                        'mapper': lambda x: ', '.join(
                            [f'0x{handle:04X}:{value.hex()}' for handle, value in x]
                        )
                    },
                ),
            ],
            '  ',
        )
        return result


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Read_Request(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.4.3 Read Request
    '''

    attribute_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Read_Response(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.4.4 Read Response
    '''

    attribute_value: bytes = dataclasses.field(metadata=hci.metadata("*"))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Read_Blob_Request(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.4.5 Read Blob Request
    '''

    attribute_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    value_offset: int = dataclasses.field(metadata=hci.metadata(2))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Read_Blob_Response(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.4.6 Read Blob Response
    '''

    part_attribute_value: bytes = dataclasses.field(metadata=hci.metadata("*"))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Read_Multiple_Request(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.4.7 Read Multiple Request
    '''

    set_of_handles: Sequence[int] = dataclasses.field(metadata=_SET_OF_HANDLES_METADATA)


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Read_Multiple_Response(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.4.8 Read Multiple Response
    '''

    set_of_values: bytes = dataclasses.field(metadata=hci.metadata("*"))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Read_By_Group_Type_Request(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.4.9 Read by Group Type Request
    '''

    starting_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    ending_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    attribute_group_type: UUID = dataclasses.field(
        metadata=hci.metadata(UUID.parse_uuid)
    )


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Read_By_Group_Type_Response(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.4.10 Read by Group Type Response
    '''

    length: int = dataclasses.field(metadata=hci.metadata(1))
    attribute_data_list: bytes = dataclasses.field(metadata=hci.metadata("*"))
    attributes: list[tuple[int, int, bytes]] = dataclasses.field(init=False)

    def __post_init__(self) -> None:
        self.attributes = []
        offset = 0
        while self.length != 0 and offset + self.length <= len(
            self.attribute_data_list
        ):
            attribute_handle, end_group_handle = struct.unpack_from(
                '<HH', self.attribute_data_list, offset
            )
            attribute_value = self.attribute_data_list[
                offset + 4 : offset + self.length
            ]
            self.attributes.append(
                (attribute_handle, end_group_handle, attribute_value)
            )
            offset += self.length

    def __str__(self):
        result = color(self.name, 'yellow')
        result += ':\n' + HCI_Object.format_fields(
            self.__dict__,
            [
                ('length', 1),
                (
                    'attributes',
                    {
                        'mapper': lambda x: ', '.join(
                            [
                                f'0x{handle:04X}-0x{end:04X}:{value.hex()}'
                                for handle, end, value in x
                            ]
                        )
                    },
                ),
            ],
            '  ',
        )
        return result


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Read_Multiple_Variable_Request(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.4.11 Read Multiple Variable Request
    '''

    set_of_handles: Sequence[int] = dataclasses.field(metadata=_SET_OF_HANDLES_METADATA)


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Read_Multiple_Variable_Response(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.4.12 Read Multiple Variable Response
    '''

    @classmethod
    def _parse_length_value_tuples(
        cls, data: bytes, offset: int
    ) -> tuple[int, list[tuple[int, bytes]]]:
        length_value_tuple_list: list[tuple[int, bytes]] = []
        while offset < len(data):
            length = struct.unpack_from('<H', data, offset)[0]
            length_value_tuple_list.append(
                (length, data[offset + 2 : offset + 2 + length])
            )
            offset += 2 + length
        return (len(data), length_value_tuple_list)

    length_value_tuple_list: Sequence[tuple[int, bytes]] = dataclasses.field(
        metadata=hci.metadata(
            {
                'parser': lambda data, offset: ATT_Read_Multiple_Variable_Response._parse_length_value_tuples(
                    data, offset
                ),
                'serializer': lambda length_value_tuple_list: b''.join(
                    [
                        struct.pack('<H', length) + value
                        for length, value in length_value_tuple_list
                    ]
                ),
            }
        )
    )


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Write_Request(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.5.1 Write Request
    '''

    attribute_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    attribute_value: bytes = dataclasses.field(metadata=hci.metadata("*"))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Write_Response(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.5.2 Write Response
    '''


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Write_Command(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.5.3 Write Command
    '''

    attribute_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    attribute_value: bytes = dataclasses.field(metadata=hci.metadata("*"))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Signed_Write_Command(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.5.4 Signed Write Command
    '''

    attribute_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    attribute_value: bytes = dataclasses.field(metadata=hci.metadata("*"))
    # TODO: authentication_signature


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Prepare_Write_Request(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.6.1 Prepare Write Request
    '''

    attribute_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    value_offset: int = dataclasses.field(metadata=hci.metadata(2))
    part_attribute_value: bytes = dataclasses.field(metadata=hci.metadata("*"))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Prepare_Write_Response(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.6.2 Prepare Write Response
    '''

    attribute_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    value_offset: int = dataclasses.field(metadata=hci.metadata(2))
    part_attribute_value: bytes = dataclasses.field(metadata=hci.metadata("*"))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Execute_Write_Request(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.6.3 Execute Write Request
    '''

    flags: int = dataclasses.field(metadata=hci.metadata(1))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Execute_Write_Response(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.6.4 Execute Write Response
    '''


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Handle_Value_Notification(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.7.1 Handle Value Notification
    '''

    attribute_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    attribute_value: bytes = dataclasses.field(metadata=hci.metadata("*"))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Handle_Value_Indication(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.7.2 Handle Value Indication
    '''

    attribute_handle: int = dataclasses.field(metadata=hci.metadata(HANDLE_FIELD_SPEC))
    attribute_value: bytes = dataclasses.field(metadata=hci.metadata("*"))


# -----------------------------------------------------------------------------
@ATT_PDU.subclass
@dataclasses.dataclass
class ATT_Handle_Value_Confirmation(ATT_PDU):
    '''
    See Bluetooth spec @ Vol 3, Part F - 3.4.7.3 Handle Value Confirmation
    '''


# -----------------------------------------------------------------------------
class AttributeValue(Generic[_T]):
    '''
    Attribute value where reading and/or writing is delegated to functions
    passed as arguments to the constructor.
    '''

    def __init__(
        self,
        read: (
            Callable[[Connection], _T] | Callable[[Connection], Awaitable[_T]] | None
        ) = None,
        write: (
            Callable[[Connection, _T], None]
            | Callable[[Connection, _T], Awaitable[None]]
            | None
        ) = None,
    ):
        self._read = read
        self._write = write

    def read(self, connection: Connection) -> _T | Awaitable[_T]:
        if self._read is None:
            raise InvalidOperationError('AttributeValue has no read function')
        return self._read(connection)

    def write(self, connection: Connection, value: _T) -> Awaitable[None] | None:
        if self._write is None:
            raise InvalidOperationError('AttributeValue has no write function')
        return self._write(connection, value)


# -----------------------------------------------------------------------------
class AttributeValueV2(Generic[_T]):
    '''
    Attribute value compatible with enhanced bearers.

    The only difference between AttributeValue and AttributeValueV2 is that the actual
    bearer (ACL connection for un-enhanced bearer, L2CAP channel for enhanced bearer)
    will be passed into read and write callbacks in V2, while in V1 it is always
    the base ACL connection.

    This is only required when attributes must distinguish bearers, otherwise normal
    `AttributeValue` objects are also applicable in enhanced bearers.
    '''

    def __init__(
        self,
        read: Callable[[Bearer], Awaitable[_T]] | Callable[[Bearer], _T] | None = None,
        write: (
            Callable[[Bearer, _T], Awaitable[None]]
            | Callable[[Bearer, _T], None]
            | None
        ) = None,
    ):
        self._read = read
        self._write = write

    def read(self, bearer: Bearer) -> _T | Awaitable[_T]:
        if self._read is None:
            raise InvalidOperationError('AttributeValue has no read function')
        return self._read(bearer)

    def write(self, bearer: Bearer, value: _T) -> Awaitable[None] | None:
        if self._write is None:
            raise InvalidOperationError('AttributeValue has no write function')
        return self._write(bearer, value)


# -----------------------------------------------------------------------------
class Attribute(utils.EventEmitter, Generic[_T]):
    class Permissions(enum.IntFlag):
        READABLE = 0x01
        WRITEABLE = 0x02
        READ_REQUIRES_ENCRYPTION = 0x04
        WRITE_REQUIRES_ENCRYPTION = 0x08
        READ_REQUIRES_AUTHENTICATION = 0x10
        WRITE_REQUIRES_AUTHENTICATION = 0x20
        READ_REQUIRES_AUTHORIZATION = 0x40
        WRITE_REQUIRES_AUTHORIZATION = 0x80

        @classmethod
        def from_string(cls, permissions_str: str) -> Attribute.Permissions:
            try:
                return functools.reduce(
                    lambda x, y: x | Attribute.Permissions[y],
                    permissions_str.replace('|', ',').split(","),
                    Attribute.Permissions(0),
                )
            except TypeError as exc:
                # The check for `p.name is not None` here is needed because for InFlag
                # enums, the .name property can be None, when the enum value is 0,
                # so the type hint for .name is Optional[str].
                enum_list: list[str] = [p.name for p in cls if p.name is not None]
                enum_list_str = ",".join(enum_list)
                raise TypeError(
                    f"Attribute::permissions error:\nExpected a string containing any of the keys, separated by commas: {enum_list_str}\nGot: {permissions_str}"
                ) from exc

    # Permission flags(legacy-use only)
    READABLE = Permissions.READABLE
    WRITEABLE = Permissions.WRITEABLE
    READ_REQUIRES_ENCRYPTION = Permissions.READ_REQUIRES_ENCRYPTION
    WRITE_REQUIRES_ENCRYPTION = Permissions.WRITE_REQUIRES_ENCRYPTION
    READ_REQUIRES_AUTHENTICATION = Permissions.READ_REQUIRES_AUTHENTICATION
    WRITE_REQUIRES_AUTHENTICATION = Permissions.WRITE_REQUIRES_AUTHENTICATION
    READ_REQUIRES_AUTHORIZATION = Permissions.READ_REQUIRES_AUTHORIZATION
    WRITE_REQUIRES_AUTHORIZATION = Permissions.WRITE_REQUIRES_AUTHORIZATION

    EVENT_READ = "read"
    EVENT_WRITE = "write"

    value: AttributeValue[_T] | _T | None

    def __init__(
        self,
        attribute_type: str | bytes | UUID,
        permissions: str | Attribute.Permissions,
        value: AttributeValue[_T] | _T | None = None,
    ) -> None:
        utils.EventEmitter.__init__(self)
        self.handle = 0
        self.end_group_handle = 0
        if isinstance(permissions, str):
            self.permissions = Attribute.Permissions.from_string(permissions)
        else:
            self.permissions = permissions

        # Convert the type to a UUID object if it isn't already
        if isinstance(attribute_type, str):
            self.type = UUID(attribute_type)
        elif isinstance(attribute_type, bytes):
            self.type = UUID.from_bytes(attribute_type)
        else:
            self.type = attribute_type

        self.value = value

    def encode_value(self, value: _T) -> bytes:
        return value  # type: ignore

    def decode_value(self, value: bytes) -> _T:
        return value  # type: ignore

    async def read_value(self, bearer: Bearer) -> bytes:
        connection = bearer.connection if is_enhanced_bearer(bearer) else bearer
        if (
            (self.permissions & self.READ_REQUIRES_ENCRYPTION)
            and connection is not None
            and not connection.encryption
        ):
            raise ATT_Error(
                error_code=ATT_INSUFFICIENT_ENCRYPTION_ERROR, att_handle=self.handle
            )
        if (
            (self.permissions & self.READ_REQUIRES_AUTHENTICATION)
            and connection is not None
            and not connection.authenticated
        ):
            raise ATT_Error(
                error_code=ATT_INSUFFICIENT_AUTHENTICATION_ERROR, att_handle=self.handle
            )
        if self.permissions & self.READ_REQUIRES_AUTHORIZATION:
            # TODO: handle authorization better
            raise ATT_Error(
                error_code=ATT_INSUFFICIENT_AUTHORIZATION_ERROR, att_handle=self.handle
            )

        value: _T | None
        if isinstance(self.value, AttributeValue):
            try:
                read_value = self.value.read(connection)
                if inspect.isawaitable(read_value):
                    value = await read_value
                else:
                    value = read_value
            except ATT_Error as error:
                raise ATT_Error(
                    error_code=error.error_code, att_handle=self.handle
                ) from error
        elif isinstance(self.value, AttributeValueV2):
            try:
                read_value = self.value.read(bearer)
                if inspect.isawaitable(read_value):
                    value = await read_value
                else:
                    value = read_value
            except ATT_Error as error:
                raise ATT_Error(
                    error_code=error.error_code, att_handle=self.handle
                ) from error
        else:
            value = self.value

        self.emit(self.EVENT_READ, connection, b'' if value is None else value)

        return b'' if value is None else self.encode_value(value)

    async def write_value(self, bearer: Bearer, value: bytes) -> None:
        connection = bearer.connection if is_enhanced_bearer(bearer) else bearer
        if (
            (self.permissions & self.WRITE_REQUIRES_ENCRYPTION)
            and connection is not None
            and not connection.encryption
        ):
            raise ATT_Error(
                error_code=ATT_INSUFFICIENT_ENCRYPTION_ERROR, att_handle=self.handle
            )
        if (
            (self.permissions & self.WRITE_REQUIRES_AUTHENTICATION)
            and connection is not None
            and not connection.authenticated
        ):
            raise ATT_Error(
                error_code=ATT_INSUFFICIENT_AUTHENTICATION_ERROR, att_handle=self.handle
            )
        if self.permissions & self.WRITE_REQUIRES_AUTHORIZATION:
            # TODO: handle authorization better
            raise ATT_Error(
                error_code=ATT_INSUFFICIENT_AUTHORIZATION_ERROR, att_handle=self.handle
            )

        decoded_value = self.decode_value(value)

        if isinstance(self.value, AttributeValue):
            try:
                result = self.value.write(connection, decoded_value)
                if inspect.isawaitable(result):
                    await result
            except ATT_Error as error:
                raise ATT_Error(
                    error_code=error.error_code, att_handle=self.handle
                ) from error
        elif isinstance(self.value, AttributeValueV2):
            try:
                result = self.value.write(bearer, decoded_value)
                if inspect.isawaitable(result):
                    await result
            except ATT_Error as error:
                raise ATT_Error(
                    error_code=error.error_code, att_handle=self.handle
                ) from error
        else:
            self.value = decoded_value

        self.emit(self.EVENT_WRITE, connection, decoded_value)

    def __repr__(self):
        if isinstance(self.value, bytes):
            value_str = self.value.hex()
        else:
            value_str = str(self.value)
        if value_str:
            value_string = f', value={self.value.hex()}'
        else:
            value_string = ''
        return (
            f'Attribute(handle=0x{self.handle:04X}, '
            f'type={self.type}, '
            f'permissions={self.permissions}{value_string})'
        )