Ejemplo n.º 1
0
 def unsuback(self, messageId):
     header = bytearray()
     varHeader = bytearray()
     header.append(0x0B << 4)
     varHeader.extend(EncodeDecode._encodeValue(messageId))
     header.extend(EncodeDecode._encodeLength(len(varHeader)))
     self.transport.write(str(header))
     self.transport.write(str(varHeader))
Ejemplo n.º 2
0
 def unsuback(self, messageId):
     # LOG.info("Sending unsuback packet")
     header = bytearray()
     varHeader = bytearray()
     header.append(0x0B << 4)
     varHeader.extend(EncodeDecode._encodeValue(messageId))
     header.extend(EncodeDecode._encodeLength(len(varHeader)))
     self.transport.write(header)
     self.transport.write(varHeader)
Ejemplo n.º 3
0
 def pubcomp(self, messageId):
     # LOG.warn('Sending pubcomp packet')
     header = bytearray()
     varHeader = bytearray()
     header.append(0x07 << 4)
     varHeader.extend(EncodeDecode._encodeValue(messageId))
     header.extend(EncodeDecode._encodeLength(len(varHeader)))
     self.transport.write(header)
     self.transport.write(varHeader)
Ejemplo n.º 4
0
 def _event_unsubscribe(self, packet, _qos, _dup, _retain):
     messageId = EncodeDecode._decodeValue(packet[:2])
     packet = packet[2:]
     #  Extract the unsubscribing topics
     topics = []
     while len(packet):
         topic = EncodeDecode._decodeString(packet)
         packet = packet[len(topic) + 2:]
         topics.append(topic)
     LOG.info('Mqtt UnSubscribe: {}'.format(topics))
     self.unsubscribeReceived(topics, messageId)
Ejemplo n.º 5
0
 def suback(self, grantedQos, messageId):
     header = bytearray()
     varHeader = bytearray()
     payload = bytearray()
     header.append(0x09 << 4)
     varHeader.extend(EncodeDecode._encodeValue(messageId))
     for i in grantedQos:
         payload.append(i)
     header.extend(EncodeDecode._encodeLength(len(varHeader) + len(payload)))
     self.transport.write(str(header))
     self.transport.write(str(varHeader))
     self.transport.write(str(payload))
Ejemplo n.º 6
0
 def _event_subscribe(self, packet, qos, _dup, _retain):
     messageId = EncodeDecode._decodeValue(packet[:2])
     packet = packet[2:]
     topics = []
     while len(packet):
         topic = EncodeDecode._decodeString(packet)
         packet = packet[len(topic) + 2:]
         qos = packet[0]
         packet = packet[1:]
         #  Add them to the list of (topic, qos)s
         topics.append((topic, qos))
     LOG.info('Mqtt Subscribe: {}'.format(topics))
     self.subscribeReceived(topics, messageId)
Ejemplo n.º 7
0
 def _event_publish(self, packet, qos, dup, retain):
     #  Extract the topic name
     topic = EncodeDecode._decodeString(packet)
     packet = packet[len(topic) + 2:]
     #  Extract the message ID if appropriate
     messageId = None
     if qos > 0:
         messageId = EncodeDecode._decodeValue(packet[:2])
         packet = packet[2:]
     #  Extract the message
     #  Whatever remains is the message
     message = str(packet)
     #  LOG.info('Mqtt Publish: {}\n\t{}'.format(topic, message))
     self.publishReceived(topic, message, qos, dup, retain, messageId)
Ejemplo n.º 8
0
 def publish(self, p_topic, p_message, qosLevel=0, retain=False, dup=False, messageId=None):
     # LOG.info("Sending publish packet\n\tTopic: {};\n\tAddr: {};".format(p_topic, self.m_broker.BrokerAddress))
     l_varHeader = bytearray()
     l_payload = bytearray()
     #  Type = publish
     l_varHeader.extend(EncodeDecode._encodeString(p_topic))
     if qosLevel > 0:
         if messageId is not None:
             l_varHeader.extend(EncodeDecode._encodeValue(messageId))
         else:
             l_varHeader.extend(EncodeDecode._encodeValue(random.randint(1, 0xFFFF)))
     l_payload.extend(EncodeDecode._put_string(p_message))
     # l_payload.extend(p_message)
     l_fixHeader = self._build_fixed_header(0x03, len(l_varHeader) + len(l_payload), dup, qosLevel, retain)
     self._send_transport(l_fixHeader, l_varHeader, l_payload)
Ejemplo n.º 9
0
 def unsubscribe(self, topic, messageId = None):
     LOG.info("Sending unsubscribe packet")
     header = bytearray()
     varHeader = bytearray()
     payload = bytearray()
     header.append(0x0A << 4 | 0x01 << 1)
     if messageId is not None:
         varHeader.extend(EncodeDecode._encodeValue(self.messageID))
     else:
         varHeader.extend(EncodeDecode._encodeValue(random.randint(1, 0xFFFF)))
     payload.extend(EncodeDecode._encodeString(topic))
     header.extend(EncodeDecode._encodeLength(len(payload) + len(varHeader)))
     self.transport.write(str(header))
     self.transport.write(str(varHeader))
     self.transport.write(str(payload))
Ejemplo n.º 10
0
    def _accumulatePacket(self, p_data):
        self.m_buffer.extend(p_data)
        l_length = None
        while len(self.m_buffer):
            if l_length is None:
                #  Start on a new packet
                #  Haven't got enough data to start a new packet, wait for some more
                if len(self.m_buffer) < 2:
                    break
                lenLen = 1
                #  Calculate the length of the length field
                while lenLen < len(self.m_buffer):
                    if not self.m_buffer[lenLen] & 0x80:
                        break
                    lenLen += 1
                #  We still haven't got all of the remaining length field
                if lenLen < len(self.m_buffer) and self.m_buffer[lenLen] & 0x80:
                    #  LOG.warn('### Early return {}'.format(PrintBytes(self.m_buffer)))
                    return
                l_length = EncodeDecode._decodeLength(self.m_buffer[1:])

            if len(self.m_buffer) >= l_length + lenLen + 1:
                chunk = self.m_buffer[:l_length + lenLen + 1]
                self._processPacket(chunk)
                self.m_buffer = self.m_buffer[l_length + lenLen + 1:]
                l_length = None
            else:
                #  LOG.warn('### exit without processing {}'.format(PrintBytes(self.m_buffer)))
                break
Ejemplo n.º 11
0
 def subscribe(self, p_topic, requestedQoS=0, messageId=None):
     """
     Only supports QoS = 0 subscribes
     Only supports one subscription per message
     """
     # LOG.info("Sending subscribe packet - Topic: {}\n\tAddr: {}".format(p_topic, self.m_broker.BrokerAddress))
     l_varHeader = bytearray()
     l_payload = bytearray()
     #  Type = subscribe, QoS = 1
     if messageId is None:
         l_varHeader.extend(EncodeDecode._encodeValue(random.randint(1, 0xFFFF)))
     else:
         l_varHeader.extend(EncodeDecode._encodeValue(messageId))
     l_payload.extend(EncodeDecode._encodeString(p_topic))
     l_payload.append(requestedQoS)
     l_fixHeader = self._build_fixed_header(0x08, len(l_varHeader) + len(l_payload), qosLevel=1)
     self._send_transport(l_fixHeader, l_varHeader, l_payload)
Ejemplo n.º 12
0
 def _build_connect(self, p_broker, p_mqtt):
     """
     """
     l_varHeader = bytearray()
     l_payload = bytearray()
     l_varHeader.extend(EncodeDecode._encodeString("MQTT"))
     l_varHeader.append(4)
     varLogin = 0
     if p_broker.UserName is not None:
         varLogin += 2
     if p_broker.Password is not None:
         varLogin += 1
     if p_broker.WillMessage is None or p_broker.WillMessage == '' or p_broker.WillTopic is None:
         #  Clean start, no will message
         l_varHeader.append(varLogin << 6 | 0 << 2 | 1 << 1)
     else:
         l_varHeader.append(varLogin << 6 | p_broker.WillRetain << 5 | p_broker.WillQoS << 3 | 1 << 2 | 1 << 1)
     l_varHeader.extend(EncodeDecode._encodeValue(int(p_broker.Keepalive / 1000)))
     l_payload.extend(EncodeDecode._encodeString(p_mqtt.ClientID))
     if (p_broker.WillMessage is not None or p_broker.WillMessage != '') and p_broker.WillTopic is not None:
         # LOG.debug('Adding last will testiment {}'.format(p_broker.WillMessage + p_broker.WillTopic))
         l_payload.extend(EncodeDecode._encodeString(p_broker.WillTopic))
         l_payload.extend(EncodeDecode._encodeString(p_broker.WillMessage))
     if p_broker.UserName is not None and len(p_broker.UserName) > 0:
         LOG.debug('Adding username "{}"'.format(p_broker.UserName))
         l_payload.extend(EncodeDecode._encodeString(p_broker.UserName))
     if p_broker.Password is not None and len(p_broker.Password) > 0:
         LOG.debug('Adding password "{}"'.format(p_broker.Password))
         l_payload.extend(EncodeDecode._encodeString(p_broker.Password))
     l_fixHeader = self._build_fixed_header(0x01, len(l_varHeader) + len(l_payload))
     return (l_fixHeader, l_varHeader, l_payload)
Ejemplo n.º 13
0
 def connack(self, status):
     LOG.warn('Got connect ack packet')
     header = bytearray()
     payload = bytearray()
     header.append(0x02 << 4)
     payload.append(status)
     header.extend(EncodeDecode._encodeLength(len(payload)))
     self.transport.write(str(header))
     self.transport.write(str(payload))
Ejemplo n.º 14
0
 def _event_suback(self, packet, _qos, _dup, _retain):
     messageId = EncodeDecode._decodeValue(packet[:2])
     packet = packet[2:]
     #  Extract the granted QoS levels
     grantedQos = []
     while len(packet):
         grantedQos.append(packet[0])
         packet = packet[1:]
     self.subackReceived(grantedQos, messageId)
Ejemplo n.º 15
0
 def publish(self, p_topic, p_message, qosLevel = 0, retain = False, dup = False, messageId = None):
     #  LOG.info("Sending publish packet - Topic: {}".format(p_topic))
     header = bytearray()
     varHeader = bytearray()
     payload = bytearray()
     #  Type = publish
     header.append(0x03 << 4 | dup << 3 | qosLevel << 1 | retain)
     varHeader.extend(EncodeDecode._encodeString(p_topic))
     if qosLevel > 0:
         if messageId is not None:
             varHeader.extend(EncodeDecode._encodeValue(messageId))
         else:
             varHeader.extend(EncodeDecode._encodeValue(random.randint(1, 0xFFFF)))
     payload.extend(p_message)
     header.extend(EncodeDecode._encodeLength(len(varHeader) + len(payload)))
     self.transport.write(str(header))
     self.transport.write(str(varHeader))
     self.transport.write(str(payload))
Ejemplo n.º 16
0
 def _event_suback(self, packet, _qos, _dup, _retain):
     messageId = EncodeDecode._decodeValue(packet[:2])
     packet = packet[2:]
     # LOG.info('Event SubAck received - MsgID:{}  Acks: {} {}\n\tAddr:{}'.format(messageId, len(packet), packet, self.m_broker.BrokerAddress))
     #  Extract the granted QoS levels
     grantedQos = []
     while len(packet):
         grantedQos.append(packet[0])
         packet = packet[1:]
     self.subackReceived(grantedQos, messageId)
Ejemplo n.º 17
0
 def subscribe(self, p_topic, requestedQoS = 0, messageId = None):
     """
     Only supports QoS = 0 subscribes
     Only supports one subscription per message
     """
     LOG.info("Sending subscribe packet - Topic: {}".format(p_topic))
     header = bytearray()
     varHeader = bytearray()
     payload = bytearray()
     #  Type = subscribe, QoS = 1
     header.append(0x08 << 4 | 0x01 << 1)
     if messageId is None:
         varHeader.extend(EncodeDecode._encodeValue(random.randint(1, 0xFFFF)))
     else:
         varHeader.extend(EncodeDecode._encodeValue(messageId))
     payload.extend(EncodeDecode._encodeString(p_topic))
     payload.append(requestedQoS)
     header.extend(EncodeDecode._encodeLength(len(varHeader) + len(payload)))
     self.transport.write(str(header))
     self.transport.write(str(varHeader))
     self.transport.write(str(payload))
Ejemplo n.º 18
0
    def _event_publish(self, packet, qos, dup, retain):
        """ Receive a "Published" message

        Here we get a published message from the broker.
        Extract the parts of the packet.
        @param packet: is a bytearray containing the variable header and payload combined.
        """
        #  Extract the topic portion of the packet.
        l_topic = EncodeDecode._decodeString(packet)
        packet = packet[len(l_topic) + 2:]
        #  Extract the message ID if appropriate
        messageId = None
        if qos > 0:
            messageId = EncodeDecode._decodeValue(packet[:2])
            packet = packet[2:]
            LOG.debug('Publish MsgID:{}'.format(messageId))
        #  Extract whatever remains as the message
        l_json = EncodeDecode._get_string(packet)
        # l_json = packet.decode('utf-8')
        l_message = json_tools.decode_json_unicode(l_json)
        # l_topic is a string
        # l_message is a string
        self.publishReceived(l_topic, l_message, qos, dup, retain, messageId)
Ejemplo n.º 19
0
 def connect(self, p_clientID, keepalive = 3000,
             willTopic = None, willMessage = None, willQoS = 0, willRetain = False,
             cleanStart = True,
             username = None,
             password = None
             ):
     """
     DBK - Modified this packet to add username and password flags and fields (2016-01-22)
     """
     LOG.info("Sending 'connect' packet - ID: {}".format(p_clientID))
     header = bytearray()
     varHeader = bytearray()
     payload = bytearray()
     varHeader.extend(EncodeDecode._encodeString("MQIsdp"))
     varHeader.append(3)
     varLogin = 0
     if username is not None:
         varLogin += 2
     if password is not None:
         varLogin += 1
     if willMessage is None or willTopic is None:
         #  Clean start, no will message
         varHeader.append(varLogin << 6 | 0 << 2 | cleanStart << 1)
     else:
         varHeader.append(varLogin << 6 | willRetain << 5 | willQoS << 3
                          | 1 << 2 | cleanStart << 1)
     varHeader.extend(EncodeDecode._encodeValue(keepalive / 1000))
     payload.extend(EncodeDecode._encodeString(p_clientID))
     if willMessage is not None and willTopic is not None:
         LOG.debug('Adding last will testiment {}'.format(willMessage + willTopic))
         payload.extend(EncodeDecode._encodeString(willTopic))
         payload.extend(EncodeDecode._encodeString(willMessage))
     if username is not None:
         LOG.debug('Adding username {}'.format(username))
         payload.extend(EncodeDecode._encodeString(username))
     if password is not None:
         LOG.debug('Adding password {}'.format(password))
         payload.extend(EncodeDecode._encodeString(password))
     header.append(0x01 << 4)
     header.extend(EncodeDecode._encodeLength(len(varHeader) + len(payload)))
     self.transport.write(str(header))
     self.transport.write(str(varHeader))
     self.transport.write(str(payload))
Ejemplo n.º 20
0
 def _event_connect(self, packet, _qos, _dup, _retain):
     """This will decode a received 'connect' packet.
     """
     LOG.info('ProtocolEvent "Connect Packet" received.')
     #  Strip variable header
     packet = packet[10:]
     #  Extract the connect flags
     f_username = packet[0] & 0x80 == 0x80
     f_password = packet[0] & 0x40 == 0x40
     f_willRetain = packet[0] & 0x20 == 0x20
     l_willQos = packet[0] & 0x18 >> 3
     f_willFlag = packet[0] & 0x04 == 0x04
     f_cleanStart = packet[0] & 0x02 == 0x02
     packet = packet[1:]
     keepalive = EncodeDecode._decodeValue(packet[:2])  # Extract the keepalive period
     packet = packet[2:]
     clientID = EncodeDecode._decodeString(packet)  # Extract the client id_event_connect
     packet = packet[len(clientID) + 2:]
     # Extract the will topic and message, if applicable
     l_willTopic = None
     l_willMessage = None
     if f_willFlag:
         #  Extract the will topic
         l_willTopic = EncodeDecode._decodeString(packet)
         packet = packet[len(l_willTopic) + 2:]
         #  Extract the will message
         #  Whatever remains is the will message
         #  ##  l_willMessage = packet
         l_willMessage = EncodeDecode._decodeString(packet)
         packet = packet[len(l_willMessage) + 2:]
     l_username = None
     if f_username:  # Extract user name if one is present.
         l_username = EncodeDecode._decodeString(packet)
         packet = packet[len(l_username) + 2:]
     l_password = None
     if f_password:  # Extract password if one is present.
         l_password = EncodeDecode._decodeString(packet)
         packet = packet[len(l_password) + 2:]
     LOG.info('Mqtt Connected.')
     self.connectReceived(clientID, keepalive, l_willTopic,
                          l_willMessage, l_willQos, f_willRetain,
                          f_cleanStart, l_username, l_password)
Ejemplo n.º 21
0
 def disconnect(self):
     LOG.info("Sending disconnect packet")
     header = bytearray()
     header.append(0x0E << 4)
     header.extend(EncodeDecode._encodeLength(0))
     self.transport.write(str(header))
Ejemplo n.º 22
0
 def _event_pubcomp(self, packet, _qos, _dup, _retain):
     messageId = EncodeDecode._decodeValue(packet[:2])
     self.pubcompReceived(messageId)
Ejemplo n.º 23
0
 def _event_puback(self, packet, _qos, _dup, _retain):
     LOG.info('ProtocolEvent "PubAck Packet" received: {} {}\n\tAddr:{}'.format(len(packet), packet, self.m_broker.BrokerAddress))
     #  Extract the message ID
     messageId = EncodeDecode._decodeValue(packet[:2])
     self.pubackReceived(messageId)
Ejemplo n.º 24
0
 def pingresp(self):
     #  LOG.warn('Got ping ack packet')
     header = bytearray()
     header.append(0x0D << 4)
     header.extend(EncodeDecode._encodeLength(0))
     self.transport.write(str(header))
Ejemplo n.º 25
0
 def _event_pubcomp(self, packet, _qos, _dup, _retain):
     LOG.info('Event PubComp received: {} {}'.format(len(packet), packet))
     messageId = EncodeDecode._decodeValue(packet[:2])
     self.pubcompReceived(messageId)
Ejemplo n.º 26
0
 def _event_unsuback(self, packet, _qos, _dup, _retain):
     LOG.info('Event UnsubAck received: {} {}'.format(len(packet), packet))
     messageId = EncodeDecode._decodeValue(packet[:2])
     self.unsubackReceived(messageId)
Ejemplo n.º 27
0
 def _build_fixed_header(self, p_packet_type, p_remaining_length, dup=0, qosLevel=0, retain=0):
     l_header = bytearray()
     l_header.append((p_packet_type & 0x0f) << 4 | (dup & 0x01) << 3 | (qosLevel & 0x03) << 1 | (retain & 0x01))
     l_header.extend(EncodeDecode._encodeLength(p_remaining_length))
     return l_header
Ejemplo n.º 28
0
 def _event_puback(self, packet, _qos, _dup, _retain):
     #  Extract the message ID
     messageId = EncodeDecode._decodeValue(packet[:2])
     self.pubackReceived(messageId)
Ejemplo n.º 29
0
 def _event_unsuback(self, packet, _qos, _dup, _retain):
     messageId = EncodeDecode._decodeValue(packet[:2])
     self.unsubackReceived(messageId)