__init__.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2019 Scott Shawcroft for Adafruit Industries 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in 13 # all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 # THE SOFTWARE. 22 """ 23 24 This module provides core Unique ID (UUID) classes. 25 26 """ 27 28 import struct 29 30 import _bleio 31 32 __version__ = "0.0.0-auto.0" 33 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git" 34 35 36 class UUID: 37 """Top level UUID""" 38 39 # TODO: Make subclassing _bleio.UUID work so we can just use it directly. 40 # pylint: disable=no-member 41 def __hash__(self): 42 return hash(self.bleio_uuid) 43 44 def __eq__(self, other): 45 if isinstance(other, _bleio.UUID): 46 return self.bleio_uuid == other 47 if isinstance(other, UUID): 48 return self.bleio_uuid == other.bleio_uuid 49 return False 50 51 def __str__(self): 52 return str(self.bleio_uuid) 53 54 def __bytes__(self): 55 if self.bleio_uuid.size == 128: 56 return self.bleio_uuid.uuid128 57 b = bytearray(2) 58 self.bleio_uuid.pack_into(b) 59 return bytes(b) 60 61 def pack_into(self, buffer, offset=0): 62 """Packs the UUID into the buffer at the given offset.""" 63 self.bleio_uuid.pack_into(buffer, offset=offset) 64 65 66 class StandardUUID(UUID): 67 """Standard 16-bit UUID defined by the Bluetooth SIG.""" 68 69 def __init__(self, uuid16): 70 if not isinstance(uuid16, int): 71 uuid16 = struct.unpack("<H", uuid16)[0] 72 self.bleio_uuid = _bleio.UUID(uuid16) 73 self.size = 16 74 75 76 class VendorUUID(UUID): 77 """Vendor defined, 128-bit UUID.""" 78 79 def __init__(self, uuid128): 80 self.bleio_uuid = _bleio.UUID(uuid128) 81 self.size = 128