From 63345442caa466df3f6d32658e0516596bea3ee2 Mon Sep 17 00:00:00 2001 From: Shin'ichiro Kawasaki Date: Sun, 27 Aug 2023 11:25:37 +0900 Subject: [PATCH] introduce BTUUID class As a preparation to move away from bluepy, introduce a new class BTUUID. Bluepy provides its unique UUID class which handles UUIDs of Bluetooth devices well. Instead of it, introduce BTUUID which extends python standard uuid.UUID class and support Bluetooth devices. Signed-off-by: Shin'ichiro Kawasaki --- pyscrlink/scratch_link.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pyscrlink/scratch_link.py b/pyscrlink/scratch_link.py index ac7dd66..826e640 100755 --- a/pyscrlink/scratch_link.py +++ b/pyscrlink/scratch_link.py @@ -10,6 +10,7 @@ import ssl import websockets import socket import json +import uuid import base64 import logging import sys @@ -44,6 +45,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):