Coverage for src / bluetooth_sig / gatt / characteristics / alert_notification_control_point.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-18 11:17 +0000

1"""Alert Notification Control Point characteristic (0x2A44) implementation. 

2 

3Control point for enabling/disabling alert notifications and triggering immediate notifications. 

4Used by Alert Notification Service (0x1811). 

5 

6Based on Bluetooth SIG GATT Specification: 

7- Alert Notification Control Point: 2 bytes (Command ID + Category ID) 

8""" 

9 

10from __future__ import annotations 

11 

12import msgspec 

13 

14from ...types import AlertCategoryID, AlertNotificationCommandID 

15from ..context import CharacteristicContext 

16from .base import BaseCharacteristic 

17from .utils import DataParser 

18 

19 

20class AlertNotificationControlPointData(msgspec.Struct): 

21 """Alert Notification Control Point characteristic data structure.""" 

22 

23 command_id: AlertNotificationCommandID 

24 category_id: AlertCategoryID 

25 

26 

27class AlertNotificationControlPointCharacteristic(BaseCharacteristic[AlertNotificationControlPointData]): 

28 """Alert Notification Control Point characteristic (0x2A44). 

29 

30 Control point for enabling/disabling notifications and requesting immediate alerts. 

31 

32 Structure (2 bytes): 

33 - Command ID: uint8 (0=Enable New Alert, 1=Enable Unread Status, etc.) 

34 - Category ID: uint8 (0=Simple Alert, 1=Email, etc. - target category for command) 

35 

36 Commands: 

37 - 0: Enable New Incoming Alert Notification 

38 - 1: Enable Unread Category Status Notification 

39 - 2: Disable New Incoming Alert Notification 

40 - 3: Disable Unread Category Status Notification 

41 - 4: Notify New Incoming Alert immediately 

42 - 5: Notify Unread Category Status immediately 

43 

44 Used by Alert Notification Service (0x1811). 

45 """ 

46 

47 min_length: int = 2 # Command ID + Category ID 

48 allow_variable_length: bool = True # Some commands may have additional data 

49 

50 def _decode_value( 

51 self, data: bytearray, ctx: CharacteristicContext | None = None, *, validate: bool = True 

52 ) -> AlertNotificationControlPointData: 

53 """Decode Alert Notification Control Point data from bytes. 

54 

55 Args: 

56 data: Raw characteristic data (2 bytes, length validated by BaseCharacteristic) 

57 ctx: Optional characteristic context 

58 validate: Whether to perform validation (currently unused) 

59 

60 Returns: 

61 AlertNotificationControlPointData with all fields 

62 

63 Raises: 

64 ValueError: If data contains invalid values 

65 

66 """ 

67 # Parse Command ID (1 byte) 

68 command_id = AlertNotificationCommandID(DataParser.parse_int8(data, 0, signed=False)) 

69 

70 # Parse Category ID (1 byte) 

71 category_id_raw = DataParser.parse_int8(data, 1, signed=False) 

72 category_id = AlertCategoryID(category_id_raw) 

73 

74 return AlertNotificationControlPointData( 

75 command_id=command_id, 

76 category_id=category_id, 

77 ) 

78 

79 def _encode_value(self, data: AlertNotificationControlPointData) -> bytearray: 

80 """Encode Alert Notification Control Point data to bytes. 

81 

82 Args: 

83 data: AlertNotificationControlPointData to encode 

84 

85 Returns: 

86 Encoded alert notification control point (2 bytes) 

87 

88 Raises: 

89 ValueError: If data contains invalid values 

90 

91 """ 

92 result = bytearray() 

93 result.append(int(data.command_id)) 

94 result.append(int(data.category_id)) 

95 return result