Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions bumble/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import dataclasses
import enum
import functools
import struct
from collections.abc import Iterable
from typing import (
Expand Down Expand Up @@ -273,13 +274,8 @@ def parse_uuid(cls, uuid_as_bytes: bytes, offset: int) -> tuple[int, UUID]:
def parse_uuid_2(cls, uuid_as_bytes: bytes, offset: int) -> tuple[int, UUID]:
return offset + 2, cls.from_bytes(uuid_as_bytes[offset : offset + 2])

def to_bytes(self, force_128: bool = False) -> bytes:
'''
Serialize UUID in little-endian byte-order
'''
if not force_128:
return self.uuid_bytes

@functools.cached_property
def uuid_128_bytes(self) -> bytes:
match len(self.uuid_bytes):
case 2:
return self.BASE_UUID + self.uuid_bytes + bytes([0, 0])
Expand All @@ -290,6 +286,15 @@ def to_bytes(self, force_128: bool = False) -> bytes:
case _:
assert False, "unreachable"

def to_bytes(self, force_128: bool = False) -> bytes:
'''
Serialize UUID in little-endian byte-order
'''
if not force_128:
return self.uuid_bytes

return self.uuid_128_bytes

def to_pdu_bytes(self) -> bytes:
'''
Convert to bytes for use in an ATT PDU.
Expand Down Expand Up @@ -318,15 +323,15 @@ def __bytes__(self) -> bytes:

def __eq__(self, other: object) -> bool:
if isinstance(other, UUID):
return self.to_bytes(force_128=True) == other.to_bytes(force_128=True)
return self.uuid_128_bytes == other.uuid_128_bytes

if isinstance(other, str):
return UUID(other) == self

return False

def __hash__(self) -> int:
return hash(self.uuid_bytes)
return hash(self.uuid_128_bytes)

def __str__(self) -> str:
result = self.to_hex_str(separator='-')
Expand Down
8 changes: 8 additions & 0 deletions tests/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ def test_uuid_to_hex_str() -> None:
)


# -----------------------------------------------------------------------------
def test_uuid_hash() -> None:
uuid = UUID("1234")
uuid_128_bytes = UUID.from_bytes(uuid.to_bytes(force_128=True))
assert uuid in {uuid_128_bytes}
assert uuid_128_bytes in {uuid}


# -----------------------------------------------------------------------------
def test_appearance() -> None:
a = Appearance(Appearance.Category.COMPUTER, Appearance.ComputerSubcategory.LAPTOP)
Expand Down
Loading