Coverage for src/bluetooth_sig/gatt/descriptors/report_reference.py: 79%

33 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-30 00:10 +0000

1"""Report Reference Descriptor implementation.""" 

2 

3from __future__ import annotations 

4 

5from enum import IntEnum 

6 

7import msgspec 

8 

9from ..characteristics.utils import DataParser 

10from .base import BaseDescriptor 

11 

12 

13class ReportType(IntEnum): 

14 """Report type values for Report Reference descriptor.""" 

15 

16 INPUT_REPORT = 0x01 

17 OUTPUT_REPORT = 0x02 

18 FEATURE_REPORT = 0x03 

19 

20 

21class ReportReferenceData(msgspec.Struct, frozen=True, kw_only=True): 

22 """Report Reference descriptor data.""" 

23 

24 report_id: int 

25 report_type: int 

26 

27 

28class ReportReferenceDescriptor(BaseDescriptor): 

29 """Report Reference Descriptor (0x2908). 

30 

31 Contains report ID and report type information. 

32 Used in HID (Human Interface Device) profiles. 

33 """ 

34 

35 def _has_structured_data(self) -> bool: 

36 return True 

37 

38 def _get_data_format(self) -> str: 

39 return "struct" 

40 

41 def _parse_descriptor_value(self, data: bytes) -> ReportReferenceData: 

42 """Parse Report Reference value. 

43 

44 Format: 2 bytes 

45 - Report ID (1 byte) 

46 - Report Type (1 byte) 

47 

48 Args: 

49 data: Raw bytes (should be 2 bytes) 

50 

51 Returns: 

52 ReportReferenceData with report ID and type 

53 

54 Raises: 

55 ValueError: If data is not exactly 2 bytes 

56 """ 

57 if len(data) != 2: 

58 raise ValueError(f"Report Reference data must be exactly 2 bytes, got {len(data)}") 

59 

60 return ReportReferenceData( 

61 report_id=DataParser.parse_int8(data, offset=0), 

62 report_type=DataParser.parse_int8(data, offset=1), 

63 ) 

64 

65 def get_report_id(self, data: bytes) -> int: 

66 """Get the report ID.""" 

67 parsed = self._parse_descriptor_value(data) 

68 return parsed.report_id 

69 

70 def get_report_type(self, data: bytes) -> int: 

71 """Get the report type.""" 

72 parsed = self._parse_descriptor_value(data) 

73 return parsed.report_type 

74 

75 def is_input_report(self, data: bytes) -> bool: 

76 """Check if this is an input report.""" 

77 return self.get_report_type(data) == ReportType.INPUT_REPORT 

78 

79 def is_output_report(self, data: bytes) -> bool: 

80 """Check if this is an output report.""" 

81 return self.get_report_type(data) == ReportType.OUTPUT_REPORT 

82 

83 def is_feature_report(self, data: bytes) -> bool: 

84 """Check if this is a feature report.""" 

85 return self.get_report_type(data) == ReportType.FEATURE_REPORT