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

29 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-11 20:14 +0000

1"""Server Characteristic Configuration Descriptor implementation.""" 

2 

3from __future__ import annotations 

4 

5from enum import IntFlag 

6 

7import msgspec 

8 

9from ..characteristics.utils import DataParser 

10from .base import BaseDescriptor 

11 

12 

13class SCCDFlags(IntFlag): 

14 """SCCD (Server Characteristic Configuration Descriptor) flags.""" 

15 

16 BROADCASTS_ENABLED = 0x0001 

17 

18 

19class SCCDData(msgspec.Struct, frozen=True, kw_only=True): 

20 """SCCD (Server Characteristic Configuration Descriptor) data.""" 

21 

22 broadcasts_enabled: bool 

23 

24 

25class ServerCharacteristicConfigurationDescriptor(BaseDescriptor): 

26 """Server Characteristic Configuration Descriptor (0x2903). 

27 

28 Controls server-side configuration for a characteristic. 

29 Currently only supports broadcast enable/disable. 

30 """ 

31 

32 _writable = True # SCCD is always writable 

33 

34 def _has_structured_data(self) -> bool: 

35 return True 

36 

37 def _get_data_format(self) -> str: 

38 return "uint16" 

39 

40 def _parse_descriptor_value(self, data: bytes) -> SCCDData: 

41 """Parse SCCD value into broadcast flags. 

42 

43 Args: 

44 data: Raw bytes (should be 2 bytes for uint16) 

45 

46 Returns: 

47 SCCDData with broadcast flags 

48 

49 Raises: 

50 ValueError: If data is not exactly 2 bytes 

51 """ 

52 if len(data) != 2: 

53 raise ValueError(f"SCCD data must be exactly 2 bytes, got {len(data)}") 

54 

55 # Parse as little-endian uint16 

56 value = DataParser.parse_int16(data, endian="little") 

57 

58 return SCCDData( 

59 broadcasts_enabled=bool(value & SCCDFlags.BROADCASTS_ENABLED), 

60 ) 

61 

62 @staticmethod 

63 def create_enable_broadcasts_value() -> bytes: 

64 """Create value to enable broadcasts.""" 

65 return SCCDFlags.BROADCASTS_ENABLED.to_bytes(2, "little") 

66 

67 @staticmethod 

68 def create_disable_broadcasts_value() -> bytes: 

69 """Create value to disable broadcasts.""" 

70 return (0).to_bytes(2, "little") 

71 

72 def is_broadcasts_enabled(self, data: bytes) -> bool: 

73 """Check if broadcasts are enabled.""" 

74 parsed = self._parse_descriptor_value(data) 

75 return parsed.broadcasts_enabled