BLEDBusSession: introduce BTUUID class

bluepy provides its unique UUID class which handles UUIDs of Bluetooth
devices well. Not to depend on bluepy, introduce BTUUID which extends
python standard uuid.UUID class.

Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
This commit is contained in:
Shin'ichiro Kawasaki
2023-08-27 11:25:37 +09:00
parent f79e7ac889
commit 39b157e839

View File

@@ -10,6 +10,7 @@ import ssl
import websockets
import socket
import json
import uuid
import base64
import logging
import sys
@@ -56,6 +57,24 @@ logger.propagate = False
HOSTNAME="device-manager.scratch.mit.edu"
scan_seconds=10.0
class BTUUID(uuid.UUID):
BLUETOOTH_BASE_UUID = "00001000800000805F9B34FB"
def __init__(self, val):
if isinstance(val, int):
if (val < 0) or (val > 0xFFFFFFFF):
raise ValueError(
"Short form UUIDs must be in range 0..0xFFFFFFFF")
val = "%04X" % val
else:
val = str(val)
val = val.replace("-", "")
if len(val) <= 8: # Short form
val = ("0" * (8 - len(val))) + val + self.BLUETOOTH_BASE_UUID
uuid.UUID.__init__(self, val)
class Session():
"""Base class for BTSession and BLESession"""
def __init__(self, websocket, loop):
@@ -211,12 +230,14 @@ class BLEDBusSession(Session):
if 'services' in f:
for s in f['services']:
logger.debug(f"service to check: {s}")
given_uuid = s
logger.debug(f"given UUID: {given_uuid} hash={UUID(given_uuid).__hash__()}")
given_uuid = BTUUID(s)
logger.debug(f"given UUID: {given_uuid} hash={given_uuid.__hash__()}")
dev_uuids = await dev.uuids
if not dev_uuids:
logger.debug(f"dev UUID not available")
continue
for u in dev_uuids:
for uuid in dev_uuids:
u = BTUUID(uuid)
logger.debug(f"dev UUID: {u} hash={u.__hash__()}")
logger.debug(given_uuid == u)
if given_uuid == u: