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

27 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-18 11:17 +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 # Parse as little-endian uint16 

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

54 

55 return SCCDData( 

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

57 ) 

58 

59 @staticmethod 

60 def create_enable_broadcasts_value() -> bytes: 

61 """Create value to enable broadcasts.""" 

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

63 

64 @staticmethod 

65 def create_disable_broadcasts_value() -> bytes: 

66 """Create value to disable broadcasts.""" 

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

68 

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

70 """Check if broadcasts are enabled.""" 

71 parsed = self._parse_descriptor_value(data) 

72 return parsed.broadcasts_enabled