1""" 2 Copyright (c) 2024, The OpenThread Authors. 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are met: 7 1. Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 2. Redistributions in binary form must reproduce the above copyright 10 notice, this list of conditions and the following disclaimer in the 11 documentation and/or other materials provided with the distribution. 12 3. Neither the name of the copyright holder nor the 13 names of its contributors may be used to endorse or promote products 14 derived from this software without specific prior written permission. 15 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 POSSIBILITY OF SUCH DAMAGE. 27""" 28 29 30class AdvertisedTlv: 31 """ 32 Represents a TCAT Advertisement TLV, used to advertise capabilities of a TCAT Device. 33 """ 34 35 DEVICE_TYPE_STATUS_TLV = 4 36 CAPABILITIES_TLV = 5 37 38 def __init__(self, type: int = None, size: int = None, value: bytes = None): 39 self._type: int = type 40 self._size: int = size 41 self._value: bytes = value 42 43 def __str__(self) -> str: 44 return f'TYPE 0x{self._type:02x} VALUE: {str(self._value)}' 45 46 def set_from_bytes(self, data: bytes): 47 self._type = (data[0] & 0xf0) >> 4 48 self._size = data[0] & 0xf 49 header_len = 1 50 self._value = data[header_len:header_len + self._size] 51 52 53class CapabilitiesTlv(AdvertisedTlv): 54 G = 0x80 55 L = 0x40 56 57 def __str__(self) -> str: 58 val = int.from_bytes(self._value, 'big') 59 return f'TYPE 0x{self._type:02x} G: {is_set(val, self.G)} L: {is_set(val, self.L)}' 60 61 62class DeviceTypeStatusTlv(AdvertisedTlv): 63 D = 0x80 64 R = 0x40 65 B = 0x20 66 T = 0x10 67 C = 0x08 68 S = 0x04 69 M = 0x02 70 71 def __str__(self) -> str: 72 val = int.from_bytes(self._value, 'big') 73 return f'TYPE 0x{self._type:02x} D: {is_set(val, self.D)} R: {is_set(val, self.R)} B: {is_set(val, self.B)} T: {is_set(val, self.T)} C: {is_set(val, self.C)} S: {is_set(val, self.S)} M: {is_set(val, self.M)}' 74 75 76def is_set(val, flag): 77 return 1 if val & flag else 0 78 79 80def _create_tlv(data: bytes): 81 res = None 82 header_len = 1 83 type = (data[0] & 0xf0) >> 4 84 size = data[0] & 0xf 85 value = data[header_len:header_len + size] 86 87 if type == AdvertisedTlv.DEVICE_TYPE_STATUS_TLV: 88 res = DeviceTypeStatusTlv(type, size, value) 89 elif type == AdvertisedTlv.CAPABILITIES_TLV: 90 res = CapabilitiesTlv(type, size, value) 91 else: 92 res = AdvertisedTlv(type, size, value) 93 94 return res 95 96 97def parse_tlvs(data: bytes): 98 res = [] 99 while data: 100 next_tlv = _create_tlv(data) 101 header_len = 1 102 data = data[next_tlv._size + header_len:] 103 res.append(next_tlv) 104 return res 105