]> git.michaelhowe.org Git - packages/p/paho-mqtt.git/commitdiff
Start of MQTT v3.1.1 support.
authorRoger Light <roger@atchoo.org>
Tue, 18 Feb 2014 23:32:19 +0000 (23:32 +0000)
committerRoger Light <roger@atchoo.org>
Tue, 18 Feb 2014 23:32:19 +0000 (23:32 +0000)
Change-Id: Id0a08ab798a9675ce18da4962d56172522248b5b

src/paho/mqtt/client.py
src/paho/mqtt/publish.py

index a57586a8030e89872f1a6f2633fad2afc78c8b1e..543e2654ddf66ed63db3430250370ab859b09e1a 100755 (executable)
@@ -50,10 +50,15 @@ VERSION_MINOR=4
 VERSION_REVISION=93
 VERSION_NUMBER=(VERSION_MAJOR*1000000+VERSION_MINOR*1000+VERSION_REVISION)
 
+MQTTv31 = 3
+MQTTv311 = 4
+
 if sys.version_info[0] < 3:
-    PROTOCOL_NAME = "MQIsdp"
+    PROTOCOL_NAMEv31 = "MQIsdp"
+    PROTOCOL_NAMEv311 = "MQTT"
 else:
-    PROTOCOL_NAME = b"MQIsdp"
+    PROTOCOL_NAMEv31 = b"MQIsdp"
+    PROTOCOL_NAMEv311 = b"MQTT"
 
 PROTOCOL_VERSION = 3
 
@@ -367,7 +372,7 @@ class Client(object):
       MQTT_LOG_ERR, and MQTT_LOG_DEBUG. The message itself is in buf.
 
     """
-    def __init__(self, client_id="", clean_session=True, userdata=None):
+    def __init__(self, client_id="", clean_session=True, userdata=None, protocol=MQTTv31):
         """client_id is the unique client id string used when connecting to the
         broker. If client_id is zero length or None, then one will be randomly
         generated. In this case, clean_session must be True. If this is not the
@@ -389,6 +394,7 @@ class Client(object):
         if not clean_session and (client_id == "" or client_id is None):
             raise ValueError('A client id must be provided if clean session is False.')
 
+        self._protocol = protocol
         self._userdata = userdata
         self._sock = None
         self._sockpairR, self._sockpairW = _socketpair_compat()
@@ -1654,7 +1660,14 @@ class Client(object):
         packet = bytearray()
         packet.extend(struct.pack("!B", command))
         self._pack_remaining_length(packet, remaining_length)
-        packet.extend(struct.pack("!H6sBBH", len(PROTOCOL_NAME), PROTOCOL_NAME, PROTOCOL_VERSION, connect_flags, keepalive))
+        if self._protocol == MQTTv31:
+            protocol = PROTOCOL_NAMEv31
+            proto_ver = 3
+        else:
+            protocol = PROTOCOL_NAMEv311
+            proto_ver = 4
+
+        packet.extend(struct.pack("!H"+str(len(protocol))+"sBBH", len(protocol), protocol, proto_ver, connect_flags, keepalive))
 
         self._pack_str16(packet, self._client_id)
 
index 975418dc17eefa90a484b1ea8a0e3d0d4b7e36be..c1d1a26c48d096ab1a48c3a444196fa008c26f8f 100644 (file)
@@ -63,7 +63,7 @@ def _on_publish(c, userdata, mid):
 
 
 def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60,
-             will=None, auth=None, tls=None):
+             will=None, auth=None, tls=None, protocol=mqtt.MQTTv31):
     """Publish multiple messages to a broker, then disconnect cleanly.
 
     This function creates an MQTT client, connects to a broker and publishes a
@@ -117,7 +117,7 @@ def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60,
         raise ValueError('msgs must be a list')
 
     client = mqtt.Client(client_id=client_id,
-                         userdata=msgs)
+                         userdata=msgs, protocol=protocol)
     client.on_publish = _on_publish
     client.on_connect = _on_connect
 
@@ -173,7 +173,7 @@ def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60,
 
 def single(topic, payload=None, qos=0, retain=False, hostname="localhost",
            port=1883, client_id="", keepalive=60, will=None, auth=None,
-           tls=None):
+           tls=None, protocol=mqtt.MQTTv31):
     """Publish a single message to a broker, then disconnect cleanly.
 
     This function creates an MQTT client, connects to a broker and publishes a