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

21 statements  

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

1"""Object Size characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5import msgspec 

6 

7from ...types.gatt_enums import CharacteristicRole 

8from ..context import CharacteristicContext 

9from .base import BaseCharacteristic 

10from .utils import DataParser 

11 

12 

13class ObjectSizeData(msgspec.Struct, frozen=True, kw_only=True): 

14 """Parsed data from Object Size characteristic. 

15 

16 Attributes: 

17 current_size: Current size of the object in bytes. 

18 allocated_size: Allocated size for the object in bytes. 

19 

20 """ 

21 

22 current_size: int 

23 allocated_size: int 

24 

25 

26class ObjectSizeCharacteristic(BaseCharacteristic[ObjectSizeData]): 

27 """Object Size characteristic (0x2AC0). 

28 

29 org.bluetooth.characteristic.object_size 

30 

31 Two uint32 fields representing the current size and allocated size 

32 of an object in the Object Transfer Service (OTS). 

33 """ 

34 

35 _manual_role = CharacteristicRole.INFO 

36 expected_length: int = 8 # uint32 + uint32 

37 min_length: int = 8 

38 

39 def _decode_value( 

40 self, 

41 data: bytearray, 

42 ctx: CharacteristicContext | None = None, 

43 *, 

44 validate: bool = True, 

45 ) -> ObjectSizeData: 

46 """Parse object size (two uint32 fields). 

47 

48 Args: 

49 data: Raw bytes (8 bytes). 

50 ctx: Optional CharacteristicContext. 

51 validate: Whether to validate ranges (default True). 

52 

53 Returns: 

54 ObjectSizeData with current_size and allocated_size. 

55 

56 """ 

57 current_size = DataParser.parse_int32(data, 0, signed=False) 

58 allocated_size = DataParser.parse_int32(data, 4, signed=False) 

59 return ObjectSizeData(current_size=current_size, allocated_size=allocated_size) 

60 

61 def _encode_value(self, data: ObjectSizeData) -> bytearray: 

62 """Encode object size to bytes. 

63 

64 Args: 

65 data: ObjectSizeData to encode. 

66 

67 Returns: 

68 Encoded bytes (8 bytes). 

69 

70 """ 

71 result = DataParser.encode_int32(data.current_size, signed=False) 

72 result.extend(DataParser.encode_int32(data.allocated_size, signed=False)) 

73 return result