def parseHeader(header): byte1 = upackHex(header[0]) t = unhexlify(hex((byte1 & 0xf0) >> 4)[2:].zfill(2)) dub = (byte1 & 0x08) >> 3 qos = (byte1 & 0x06) >> 1 retain = byte1 & 0x01 restLen, idx = decodeRestLen([upackHex(x) for x in header[1:]]) return t, dub, qos, retain, restLen, idx+1
def parseHeader(header): byte1 = upackHex(header[0]) t = unhexlify(hex((byte1 & 0xf0) >> 4)[2:].zfill(2)) dub = (byte1 & 0x08) >> 3 qos = (byte1 & 0x06) >> 1 retain = byte1 & 0x01 restLen, idx = decodeRestLen([upackHex(x) for x in header[1:]]) return t, dub, qos, retain, restLen, idx + 1
def subscribe(data): self.idx += 2 messageID = upackHex(data[:self.idx]) topics = [] allowedQoSs = [] while data[self.idx:]: topic = self.getIncrement(data[self.idx:]) reqQoS = upackHex(data[self.idx]) topics.append(topic) allowedQoSs.append(reqQoS) self.idx += 1 return messageID, topics, allowedQoSs
def publish(data): topic = self.getIncrement(data) messageID = 1 if 1 <= qos <= 2: messageID = upackHex(data[self.idx:self.idx + 2]) pubData = self.getIncrement(data[self.idx:]) if len(data[self.idx:]) else "" return messageID, qos, topic, pubData
def unsubscribe(data): self.idx += 2 messageID = upackHex(data[:self.idx]) topics = [] while data[self.idx:]: topic = self.getIncrement(data[self.idx:]) topics.append(topic) return messageID, topics
def connect(data): proto = self.getIncrement(data) protoVersion = upackHex(data[self.idx]) flags = upackHex(data[self.idx + 1]) keepAlive = upackHex(data[self.idx + 2:self.idx + 4]) self.idx += 4 cliId = self.getIncrement(data[self.idx:]) will = {"QoS": (flags & 0x18) >> 3, "retain": (flags & 0x20) >> 5, "topic": "", "message": ""} if flags & 0x04: will["topic"] = self.getIncrement(data[self.idx:]) will["message"] = self.getIncrement(data[self.idx:]) name = self.getIncrement(data[self.idx:]) if flags & 0x80 else "" passwd = self.getIncrement(data[self.idx:]) if flags & 0x40 else "" clean = flags & 0x02 return cliId, name, passwd, will, keepAlive, clean
def publish(data): topic = self.getIncrement(data) messageID = 1 if 1 <= qos <= 2: messageID = upackHex(data[self.idx:self.idx + 2]) pubData = self.getIncrement(data[self.idx:]) if len( data[self.idx:]) else "" return messageID, qos, topic, pubData
def connect(data): proto = self.getIncrement(data) protoVersion = upackHex(data[self.idx]) flags = upackHex(data[self.idx + 1]) keepAlive = upackHex(data[self.idx + 2:self.idx + 4]) self.idx += 4 cliId = self.getIncrement(data[self.idx:]) will = { "QoS": (flags & 0x18) >> 3, "retain": (flags & 0x20) >> 5, "topic": "", "message": "" } if flags & 0x04: will["topic"] = self.getIncrement(data[self.idx:]) will["message"] = self.getIncrement(data[self.idx:]) name = self.getIncrement(data[self.idx:]) if flags & 0x80 else "" passwd = self.getIncrement(data[self.idx:]) if flags & 0x40 else "" clean = flags & 0x02 return cliId, name, passwd, will, keepAlive, clean
def unsuback(data): messageID = upackHex(data[:2]) return messageID
def suback(data): messageID = upackHex(data[:2]) allowedQoSs = [] for q in data[2:]: allowedQoSs.append(upackHex(q)) return messageID, allowedQoSs
def pubcomp(data): messageID = upackHex(data[:2]) return messageID
def pubrel(data): messageID = upackHex(data[:2]) return messageID