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

12 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-18 11:17 +0000

1"""Object Name characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5from ..context import CharacteristicContext 

6from .base import BaseCharacteristic 

7from .utils import DataParser 

8 

9_MAX_NAME_LENGTH = 120 # Maximum UTF-8 string length per OTS spec 

10 

11 

12class ObjectNameCharacteristic(BaseCharacteristic[str]): 

13 """Object Name characteristic (0x2ABE). 

14 

15 org.bluetooth.characteristic.object_name 

16 

17 A UTF-8 string (0-120 bytes) representing the name of an object 

18 in the Object Transfer Service (OTS). 

19 """ 

20 

21 min_length: int = 0 

22 max_length: int = _MAX_NAME_LENGTH 

23 

24 def _decode_value(self, data: bytearray, ctx: CharacteristicContext | None = None, *, validate: bool = True) -> str: 

25 """Parse object name (UTF-8 string). 

26 

27 Args: 

28 data: Raw bytes (0-120 bytes). 

29 ctx: Optional CharacteristicContext. 

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

31 

32 Returns: 

33 Object name as string. 

34 

35 """ 

36 return DataParser.parse_utf8_string(data) 

37 

38 def _encode_value(self, data: str) -> bytearray: 

39 """Encode object name to bytes. 

40 

41 Args: 

42 data: Object name as string (0-120 bytes when encoded). 

43 

44 Returns: 

45 Encoded bytes. 

46 

47 """ 

48 return bytearray(data.encode("utf-8"))