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

34 statements  

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

1"""Cycling Power Feature characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5from enum import IntFlag 

6 

7import msgspec 

8 

9from ..context import CharacteristicContext 

10from .base import BaseCharacteristic 

11from .utils import DataParser 

12 

13 

14class CyclingPowerFeatures(IntFlag): 

15 """Cycling Power Feature flags as per Bluetooth SIG specification.""" 

16 

17 PEDAL_POWER_BALANCE_SUPPORTED = 0x01 

18 ACCUMULATED_ENERGY_SUPPORTED = 0x02 

19 WHEEL_REVOLUTION_DATA_SUPPORTED = 0x04 

20 CRANK_REVOLUTION_DATA_SUPPORTED = 0x08 

21 

22 

23class CyclingPowerFeatureData(msgspec.Struct, frozen=True, kw_only=True): # pylint: disable=too-few-public-methods 

24 """Parsed data from Cycling Power Feature characteristic.""" 

25 

26 features: CyclingPowerFeatures 

27 pedal_power_balance_supported: bool 

28 accumulated_energy_supported: bool 

29 wheel_revolution_data_supported: bool 

30 crank_revolution_data_supported: bool 

31 

32 

33class CyclingPowerFeatureCharacteristic(BaseCharacteristic): 

34 """Cycling Power Feature characteristic (0x2A65). 

35 

36 Used to expose the supported features of a cycling power sensor. 

37 Contains a 32-bit bitmask indicating supported measurement 

38 capabilities. 

39 """ 

40 

41 def decode_value(self, data: bytearray, ctx: CharacteristicContext | None = None) -> CyclingPowerFeatureData: 

42 """Parse cycling power feature data. 

43 

44 Format: 32-bit feature bitmask (little endian). 

45 

46 Args: 

47 data: Raw bytearray from BLE characteristic. 

48 ctx: Optional CharacteristicContext providing surrounding context (may be None). 

49 

50 Returns: 

51 CyclingPowerFeatureData containing parsed feature flags. 

52 

53 Raises: 

54 ValueError: If data format is invalid. 

55 

56 """ 

57 if len(data) < 4: 

58 raise ValueError("Cycling Power Feature data must be at least 4 bytes") 

59 

60 # Parse 32-bit unsigned integer (little endian) 

61 feature_mask: int = DataParser.parse_int32(data, 0, signed=False) 

62 

63 # Parse feature flags according to specification 

64 return CyclingPowerFeatureData( 

65 features=CyclingPowerFeatures(feature_mask), 

66 pedal_power_balance_supported=bool(feature_mask & CyclingPowerFeatures.PEDAL_POWER_BALANCE_SUPPORTED), 

67 accumulated_energy_supported=bool(feature_mask & CyclingPowerFeatures.ACCUMULATED_ENERGY_SUPPORTED), 

68 wheel_revolution_data_supported=bool(feature_mask & CyclingPowerFeatures.WHEEL_REVOLUTION_DATA_SUPPORTED), 

69 crank_revolution_data_supported=bool(feature_mask & CyclingPowerFeatures.CRANK_REVOLUTION_DATA_SUPPORTED), 

70 ) 

71 

72 def encode_value(self, data: CyclingPowerFeatureData) -> bytearray: 

73 """Encode cycling power feature value back to bytes. 

74 

75 Args: 

76 data: CyclingPowerFeatureData containing cycling power feature data 

77 

78 Returns: 

79 Encoded bytes representing the cycling power features (uint32) 

80 

81 """ 

82 # Reconstruct the features bitmap from individual flags 

83 features_bitmap = 0 

84 if data.pedal_power_balance_supported: 

85 features_bitmap |= CyclingPowerFeatures.PEDAL_POWER_BALANCE_SUPPORTED 

86 if data.accumulated_energy_supported: 

87 features_bitmap |= CyclingPowerFeatures.ACCUMULATED_ENERGY_SUPPORTED 

88 if data.wheel_revolution_data_supported: 

89 features_bitmap |= CyclingPowerFeatures.WHEEL_REVOLUTION_DATA_SUPPORTED 

90 if data.crank_revolution_data_supported: 

91 features_bitmap |= CyclingPowerFeatures.CRANK_REVOLUTION_DATA_SUPPORTED 

92 

93 return DataParser.encode_int32(features_bitmap, signed=False)