Coverage for src / bluetooth_sig / registry / uuids / protocol_identifiers.py: 91%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-11 20:14 +0000

1"""Protocol identifiers registry for Bluetooth SIG protocol definitions.""" 

2 

3from __future__ import annotations 

4 

5from bluetooth_sig.registry.base import BaseUUIDRegistry 

6from bluetooth_sig.types.registry.protocol_identifiers import ProtocolInfo 

7from bluetooth_sig.types.uuid import BluetoothUUID 

8 

9__all__ = ["ProtocolIdentifiersRegistry", "ProtocolInfo", "protocol_identifiers_registry"] 

10 

11 

12class ProtocolIdentifiersRegistry(BaseUUIDRegistry[ProtocolInfo]): 

13 """Registry for Bluetooth protocol identifiers. 

14 

15 Provides lookup of protocol information by UUID or name. 

16 Supports Classic Bluetooth protocols (L2CAP, RFCOMM, BNEP, AVDTP, etc.) 

17 and BLE protocols. 

18 """ 

19 

20 def _load_yaml_path(self) -> str: 

21 """Return the YAML file path relative to bluetooth_sig/ root.""" 

22 return "assigned_numbers/uuids/protocol_identifiers.yaml" 

23 

24 def _create_info_from_yaml(self, uuid_data: dict[str, str], uuid: BluetoothUUID) -> ProtocolInfo: 

25 """Create ProtocolInfo from YAML data.""" 

26 return ProtocolInfo( 

27 uuid=uuid, 

28 name=uuid_data["name"], 

29 ) 

30 

31 def _create_runtime_info(self, entry: object, uuid: BluetoothUUID) -> ProtocolInfo: 

32 """Create runtime ProtocolInfo from entry.""" 

33 return ProtocolInfo( 

34 uuid=uuid, 

35 name=getattr(entry, "name", ""), 

36 ) 

37 

38 def get_protocol_info(self, uuid: str | BluetoothUUID) -> ProtocolInfo | None: 

39 """Get protocol information by UUID or name. 

40 

41 Args: 

42 uuid: Protocol UUID (string, int, or BluetoothUUID) or protocol name 

43 

44 Returns: 

45 ProtocolInfo if found, None otherwise 

46 

47 Examples: 

48 >>> registry = ProtocolIdentifiersRegistry() 

49 >>> info = registry.get_protocol_info("0x0100") 

50 >>> if info: 

51 ... print(info.name) # "L2CAP" 

52 >>> info = registry.get_protocol_info("RFCOMM") 

53 >>> if info: 

54 ... print(info.uuid.short_form) # "0003" 

55 """ 

56 return self.get_info(uuid) 

57 

58 def get_protocol_info_by_name(self, name: str) -> ProtocolInfo | None: 

59 """Get protocol information by name (case insensitive). 

60 

61 Args: 

62 name: The protocol name to look up (e.g., "L2CAP", "RFCOMM") 

63 

64 Returns: 

65 ProtocolInfo if found, None otherwise 

66 """ 

67 return self.get_info(name) 

68 

69 def is_known_protocol(self, uuid: str | BluetoothUUID) -> bool: 

70 """Check if a UUID corresponds to a known protocol. 

71 

72 Args: 

73 uuid: The UUID to check 

74 

75 Returns: 

76 True if the UUID is a known protocol, False otherwise 

77 """ 

78 self._ensure_loaded() 

79 return self.get_protocol_info(uuid) is not None 

80 

81 def get_all_protocols(self) -> list[ProtocolInfo]: 

82 """Get all registered protocol identifiers. 

83 

84 Returns: 

85 List of all ProtocolInfo objects 

86 """ 

87 self._ensure_loaded() 

88 return list(self._canonical_store.values()) 

89 

90 

91# Global instance for convenience 

92protocol_identifiers_registry = ProtocolIdentifiersRegistry.get_instance()