class TLVWithSubType(GenericTLV): """Modify the :class:`GenericTLV` to a Organization Specific TLV structure. Beyond the standard TLV (type, length, value), we can also have a more specific structure, with the :attr:`value` field being splitted into a :attr:`sub_type` field and a new :attr:`sub_value` field. """ def __init__(self, tlv_type=1, sub_type=7, sub_value=None): """Create an instance and set its attributes. Args: tlv_type (int): Type used by this class. Defaults to 1. sub_type (int): Sub type value used by this class. Defaults to 7. sub_value (:class:`~pyof.foundation.basic_types.BinaryData`): Data stored by TLVWithSubType. Defaults to empty BinaryData. """ super().__init__(tlv_type) self.sub_type = sub_type self.sub_value = BinaryData() if sub_value is None else sub_value @property def value(self): """Return sub type and sub value as binary data. Returns: :class:`~pyof.foundation.basic_types.BinaryData`: BinaryData calculated. """ binary = UBInt8(self.sub_type).pack() + self.sub_value.pack() return BinaryData(binary) def unpack(self, buff, offset=0): """Unpack a binary message into this object's attributes. Unpack the binary value *buff* and update this object attributes based on the results. Args: buff (bytes): Binary data package to be unpacked. offset (int): Where to begin unpacking. Raises: Exception: If there is a struct unpacking error. """ header = UBInt16() header.unpack(buff[offset:offset+2]) self.tlv_type = header.value >> 9 length = header.value & 511 begin, end = offset + 2, offset + 2 + length sub_type = UBInt8() sub_type.unpack(buff[begin:begin+1]) self.sub_type = sub_type.value self.sub_value = BinaryData(buff[begin+1:end])
class TLVWithSubType(GenericTLV): """Modify the :class:`GenericTLV` to a Organization Specific TLV structure. Beyond the standard TLV (type, length, value), we can also have a more specific structure, with the :attr:`value` field being splitted into a :attr:`sub_type` field and a new :attr:`sub_value` field. """ def __init__(self, tlv_type=1, sub_type=7, sub_value=None): """Create an instance and set its attributes. Args: tlv_type (int): Type used by this class. Defaults to 1. sub_type (int): Sub type value used by this class. Defaults to 7. sub_value (:class:`~pyof.foundation.basic_types.BinaryData`): Data stored by TLVWithSubType. Defaults to empty BinaryData. """ super().__init__(tlv_type) self.sub_type = sub_type self.sub_value = BinaryData() if sub_value is None else sub_value @property def value(self): """Return sub type and sub value as binary data. Returns: :class:`~pyof.foundation.basic_types.BinaryData`: BinaryData calculated. """ binary = UBInt8(self.sub_type).pack() + self.sub_value.pack() return BinaryData(binary) def unpack(self, buff, offset=0): """Unpack a binary message into this object's attributes. Unpack the binary value *buff* and update this object attributes based on the results. Args: buff (bytes): Binary data package to be unpacked. offset (int): Where to begin unpacking. Raises: Exception: If there is a struct unpacking error. """ header = UBInt16() header.unpack(buff[offset:offset+2]) self.tlv_type = header.value >> 9 length = header.value & 511 begin, end = offset + 2, offset + 2 + length sub_type = UBInt8() sub_type.unpack(buff[begin:begin+1]) self.sub_type = sub_type.value self.sub_value = BinaryData(buff[begin+1:end])
class TLVWithSubType(GenericTLV): def __init__(self, type=1, sub_type=7, sub_value=BinaryData()): self.type = type self.sub_type = sub_type self.sub_value = BinaryData() @property def value(self): binary = UBInt8(self.sub_type).pack() + self.sub_value.pack() return BinaryData(binary) def unpack(self, buffer, offset=0): header = UBInt16() header.unpack(buffer[offset:offset+2]) self.type = header.value >> 9 length = header.value & 511 begin, end = offset + 2, offset + 2 + length sub_type = UBInt8() sub_type.unpack(buffer[begin:begin+1]) self.sub_type = sub_type.value self.sub_value = BinaryData(buffer[begin+1:end])