def __init__(self, data): super().__init__() self.protocol_name = None self.protocol_level = None self.connect_flags = ConnHeader.ConnectionFlags() self.keep_alive = None cursor = 0 # Parse the protocol name: it's a UTF-8 string, prepended with a 2 bytes length self.protocol_name, data_len = parse_utf8_prefixed_string(data[cursor:]) cursor += data_len # The following byte is the Protocol Level. For MQTT 3.1.1 should be 0x04 self.protocol_level = data[cursor] cursor += 1 # Connection flags flags = Byte(data[cursor]).binary cursor += 1 # Populate connection flags self.connect_flags.clean_session = flags[6] == '1' self.connect_flags.will = flags[5] == '1' self.connect_flags.will_qos = flags[3:5] self.connect_flags.will_retain = flags[2] == '1' self.connect_flags.user_name = flags[0] == '1' self.connect_flags.password = flags[1] == '1' # Keep Alive self.keep_alive = struct.unpack(">H", data[cursor:cursor + 2])[0] cursor += 2 self.length = cursor
def __init__(self, data): super().__init__(length=0) self.topics = [] cursor = 0 while cursor < len(data): topic, topic_len = parse_utf8_prefixed_string(data[cursor:]) self.topics.append(topic) cursor += topic_len # Update len self.length = cursor
def __init__(self, variable_header, data): super().__init__(length=0) self.client_identifier = None self.will_topic = None self.will_message = None self.user_name = None self.password = None cursor = 0 # The payload of the CONNECT Packet contains one or more length-prefixed fields, whose presence is # determined by the flags in the variable header. These fields, if present, MUST appear in the order # Client Identifier, Will Topic, Will Message, User Name, Password [MQTT-3.1.3-1]. # Client identifier self.client_identifier, field_len = parse_utf8_prefixed_string( data[cursor:]) cursor += field_len if variable_header.connect_flags.will: self.will_topic, field_len = parse_utf8_prefixed_string( data[cursor:]) cursor += field_len self.will_message, field_len = parse_utf8_prefixed_string( data[cursor:]) cursor += field_len if variable_header.connect_flags.user_name: self.user_name, field_len = parse_utf8_prefixed_string( data[cursor:]) cursor += field_len if variable_header.connect_flags.password: self.password, field_len = parse_utf8_prefixed_string( data[cursor:]) cursor += field_len # Update len self.length = cursor
def __init__(self, data, qos): super().__init__() self.packet_identifier = -1 self.topic_name = "" cursor = 0 self.topic_name, data_len = parse_utf8_prefixed_string(data) cursor += data_len # Packet identifier is present only if QoS > 0 if qos.value > 0: self.packet_identifier = struct.unpack(">H", data[cursor:cursor + 2])[0] cursor += 2 self.length = cursor
def __init__(self, data): super().__init__(length=0) self.topics = {} cursor = 0 # The payload of a SUBSCRIBE Packet contains a list of Topic Filters indicating the Topics to which the Client # wants to subscribe. The Topic Filters in a SUBSCRIBE packet payload MUST be UTF-8 encoded strings # We will scan the whole data byte stream until we parse all the topics. while cursor < len(data): topic, topic_len = parse_utf8_prefixed_string(data[cursor:]) cursor += topic_len # After each topic name, there is one BYTE that encodes the requested QoS for that topic subscription requested_qos = data[cursor] cursor += 1 self.topics[topic] = QoS(requested_qos) # Update len self.length = cursor