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

18 statements  

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

1"""Boot Keyboard Output Report characteristic implementation.""" 

2 

3from __future__ import annotations 

4 

5from enum import IntFlag 

6 

7from ..context import CharacteristicContext 

8from .base import BaseCharacteristic 

9 

10 

11class KeyboardLEDs(IntFlag): 

12 """Keyboard LED states bitmap.""" 

13 

14 NUM_LOCK = 0x01 

15 CAPS_LOCK = 0x02 

16 SCROLL_LOCK = 0x04 

17 COMPOSE = 0x08 

18 KANA = 0x10 

19 

20 

21class BootKeyboardOutputReportCharacteristic(BaseCharacteristic[KeyboardLEDs]): 

22 """Boot Keyboard Output Report characteristic (0x2A32). 

23 

24 org.bluetooth.characteristic.boot_keyboard_output_report 

25 

26 Contains keyboard LED states from host to keyboard following USB HID boot protocol. 

27 Format: 1 byte - LED states bitmap. 

28 

29 Spec Reference: 

30 USB HID Specification v1.11, Appendix B - Boot Interface Descriptors 

31 """ 

32 

33 min_length = 1 

34 max_length = 1 

35 allow_variable_length = False 

36 

37 def _decode_value( 

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

39 ) -> KeyboardLEDs: 

40 """Parse keyboard LED states. 

41 

42 Args: 

43 data: Raw bytearray from BLE characteristic (1 byte). 

44 ctx: Optional CharacteristicContext. 

45 validate: Whether to validate ranges (default True) 

46 

47 Returns: 

48 KeyboardLEDs with parsed LED states. 

49 """ 

50 return KeyboardLEDs(data[0]) 

51 

52 def _encode_value(self, data: KeyboardLEDs) -> bytearray: 

53 """Encode LED states to bytes. 

54 

55 Args: 

56 data: KeyboardLEDs to encode 

57 

58 validate: Whether to validate ranges (default True) 

59 

60 Returns: 

61 Encoded bytes (1 byte) 

62 """ 

63 return bytearray([int(data)])