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

44 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-03 16:41 +0000

1"""Bond Management Feature characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5from enum import IntFlag 

6 

7from ..context import CharacteristicContext 

8from .base import BaseCharacteristic 

9 

10 

11class BondManagementFeatureFlags(IntFlag): 

12 """Bond Management Feature flags as per BMS v1.0.1 Table 3.5. 

13 

14 Variable-length bit field across up to 3 octets (18 defined bits). 

15 """ 

16 

17 DELETE_REQUESTING_BR_EDR_LE = 0x000001 

18 DELETE_REQUESTING_BR_EDR_LE_AUTH = 0x000002 

19 DELETE_REQUESTING_BR_EDR = 0x000004 

20 DELETE_REQUESTING_BR_EDR_AUTH = 0x000008 

21 DELETE_REQUESTING_LE = 0x000010 

22 DELETE_REQUESTING_LE_AUTH = 0x000020 

23 DELETE_ALL_BR_EDR_LE = 0x000040 

24 DELETE_ALL_BR_EDR_LE_AUTH = 0x000080 

25 DELETE_ALL_BR_EDR = 0x000100 

26 DELETE_ALL_BR_EDR_AUTH = 0x000200 

27 DELETE_ALL_LE = 0x000400 

28 DELETE_ALL_LE_AUTH = 0x000800 

29 DELETE_ALL_EXCEPT_REQUESTING_BR_EDR_LE = 0x001000 

30 DELETE_ALL_EXCEPT_REQUESTING_BR_EDR_LE_AUTH = 0x002000 

31 DELETE_ALL_EXCEPT_REQUESTING_BR_EDR = 0x004000 

32 DELETE_ALL_EXCEPT_REQUESTING_BR_EDR_AUTH = 0x008000 

33 DELETE_ALL_EXCEPT_REQUESTING_LE = 0x010000 

34 DELETE_ALL_EXCEPT_REQUESTING_LE_AUTH = 0x020000 

35 

36 

37class BondManagementFeatureCharacteristic(BaseCharacteristic[BondManagementFeatureFlags]): 

38 """Bond Management Feature characteristic (0x2AA5). 

39 

40 org.bluetooth.characteristic.bond_management_feature 

41 

42 Variable-length bit field (1-3 octets) where each bit indicates 

43 a supported bond management operation per BMS v1.0.1 Table 3.5. 

44 Server sends only enough octets for the highest set bit. 

45 """ 

46 

47 _MAX_OCTETS = 3 

48 _BITS_PER_BYTE = 8 

49 _BYTE_MASK = 0xFF 

50 

51 min_length = 1 

52 max_length = _MAX_OCTETS 

53 allow_variable_length = True 

54 

55 def _decode_value( 

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

57 ) -> BondManagementFeatureFlags: 

58 """Decode Bond Management Feature data from variable-length bytes.""" 

59 value = 0 

60 for i, byte in enumerate(data[: self._MAX_OCTETS]): 

61 value |= byte << (self._BITS_PER_BYTE * i) 

62 return BondManagementFeatureFlags(value) 

63 

64 def _encode_value(self, data: BondManagementFeatureFlags) -> bytearray: 

65 """Encode Bond Management Feature flags to minimum required bytes.""" 

66 value = int(data) 

67 if value == 0: 

68 return bytearray(self.min_length) 

69 num_octets = (value.bit_length() + 7) // self._BITS_PER_BYTE 

70 result = bytearray(num_octets) 

71 for i in range(num_octets): 

72 result[i] = (value >> (self._BITS_PER_BYTE * i)) & self._BYTE_MASK 

73 return result