/ adafruit_ble_eddystone / __init__.py
__init__.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2020 Scott Shawcroft for Adafruit Industries LLC 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 `adafruit_ble_eddystone` 24 ================================================================================ 25 26 CircuitPython BLE library for Google's open "physical web" Eddystone. 27 28 Documented by Google here: https://github.com/google/eddystone 29 30 """ 31 32 import struct 33 34 from adafruit_ble.advertising import Advertisement 35 from adafruit_ble.advertising.standard import ServiceList, ServiceData 36 from adafruit_ble.uuid import StandardUUID 37 38 __version__ = "0.0.0-auto.0" 39 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE_Eddystone.git" 40 41 42 class _EddystoneService: 43 """Placeholder service. Not implemented.""" 44 45 # pylint: disable=too-few-public-methods 46 uuid = StandardUUID(0xFEAA) 47 48 49 class _EddystoneFrame(ServiceData): 50 """Top level advertising data field that adds the field type to bytearrays set.""" 51 52 def __init__(self): 53 super().__init__(_EddystoneService) 54 55 def __get__(self, obj, cls): 56 if obj is None: 57 return self 58 return super().__get__(obj, cls)[1:] 59 60 def __set__(self, obj, value): 61 # ServiceData requires a bytearray. 62 return super().__set__(obj, bytearray(obj.frame_type) + value) 63 64 65 class EddystoneFrameBytes: 66 """Extracts and manipulates a byte range from an EddystoneAdvertisement. For library use only. 67 68 If length is None, then the byte range must be at the end of the frame. 69 """ 70 71 def __init__(self, *, length=None, offset=0): 72 self._length = length 73 self._offset = offset 74 75 def __get__(self, obj, cls): 76 if obj is None: 77 return self 78 if self._length is not None: 79 return obj.eddystone_frame[self._offset : self._offset + self._length] 80 return obj.eddystone_frame[self._offset :] 81 82 def __set__(self, obj, value): 83 if self._length is not None: 84 if self._length != len(value): 85 raise ValueError("Value length does not match") 86 obj.eddystone_frame[self._offset : self._offset + self._length] = value 87 else: 88 obj.eddystone_frame = obj.eddystone_frame[: self._offset] + value 89 90 91 class EddystoneFrameStruct(EddystoneFrameBytes): 92 """Packs and unpacks a single value from a byte range. For library use only.""" 93 94 def __init__(self, fmt, *, offset=0): 95 self._format = fmt 96 super().__init__(offset=offset, length=struct.calcsize(self._format)) 97 98 def __get__(self, obj, cls): 99 if obj is None: 100 return self 101 return struct.unpack(self._format, super().__get__(obj, cls))[0] 102 103 def __set__(self, obj, value): 104 super().__set__(obj, struct.pack(self._format, value)) 105 106 107 class EddystoneAdvertisement(Advertisement): 108 """Top level Eddystone advertisement that manages frame type. For library use only.""" 109 110 # Subclasses must provide `match_prefixes`. 111 services = ServiceList(standard_services=[0x03], vendor_services=[0x07]) 112 eddystone_frame = _EddystoneFrame() 113 114 def __init__(self, *, minimum_size=None): 115 super().__init__() 116 self.services.append(_EddystoneService) 117 self.connectable = False 118 self.flags.general_discovery = True 119 self.flags.le_only = True 120 # self.frame_type is defined by the subclass. 121 if not self.eddystone_frame: 122 self.eddystone_frame = bytearray(minimum_size) 123 124 def __str__(self): 125 parts = [] 126 for attr in dir(self.__class__): 127 attribute_instance = getattr(self.__class__, attr) 128 if issubclass(attribute_instance.__class__, EddystoneFrameBytes): 129 value = getattr(self, attr) 130 if value is not None: 131 if isinstance(value, memoryview): 132 value = bytes(value) 133 parts.append("{}={}".format(attr, repr(value))) 134 return "<{} {} >".format(self.__class__.__name__, " ".join(parts))