From d0788450566db85baf0214098cb64a1b76406655 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Mon, 12 May 2014 22:35:23 +0100 Subject: [PATCH] [434143] Default protocol is now MQTT v3.1.1. Connecting from both client.py and publish.py will use MQTT v3.1.1 by default. Client will reconnect using MQTT v3.1 if a v3.1.1 connection fails due to the incorrect protocol version number. Bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=434143 Change-Id: I34c4288535b99c8a4f05d92f9e3c99d1fa07b5c1 --- ChangeLog.txt | 7 +++++++ README.rst | 6 +++--- src/paho/mqtt/client.py | 12 +++++++++--- src/paho/mqtt/publish.py | 4 ++-- test/paho_test.py | 6 +++--- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 380bcf1..5558be2 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,3 +1,10 @@ +v1.0 +==== + +- Default protocol is now MQTT v3.1.1. +- Client will reconnect using MQTT v3.1 if a v3.1.1 connection fails due to + the incorrect protocol version number. + v0.9.1 ====== diff --git a/README.rst b/README.rst index 75b5783..ddc5149 100644 --- a/README.rst +++ b/README.rst @@ -127,7 +127,7 @@ Client() :: - Client(client_id="", clean_session=True, userdata=None, protocol=MQTTv31) + Client(client_id="", clean_session=True, userdata=None, protocol=MQTTv311) The ``Client()`` constructor takes the following arguments: @@ -815,7 +815,7 @@ Publish a single message to a broker, then disconnect cleanly. single(topic, payload=None, qos=0, retain=False, hostname="localhost", port=1883, client_id="", keepalive=60, will=None, auth=None, tls=None, - protocol=mqtt.MQTTv31) + protocol=mqtt.MQTTv311) Function arguments @@ -892,7 +892,7 @@ Publish multiple messages to a broker, then disconnect cleanly. :: multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60, - will=None, auth=None, tls=None, protocol=mqtt.MQTTv31) + will=None, auth=None, tls=None, protocol=mqtt.MQTTv311) Function arguments '''''''''''''''''' diff --git a/src/paho/mqtt/client.py b/src/paho/mqtt/client.py index 0e421d5..1f75483 100755 --- a/src/paho/mqtt/client.py +++ b/src/paho/mqtt/client.py @@ -47,7 +47,7 @@ else: VERSION_MAJOR=0 VERSION_MINOR=9 -VERSION_REVISION=1 +VERSION_REVISION=100 VERSION_NUMBER=(VERSION_MAJOR*1000000+VERSION_MINOR*1000+VERSION_REVISION) MQTTv31 = 3 @@ -375,7 +375,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, protocol=MQTTv31): + def __init__(self, client_id="", clean_session=True, userdata=None, protocol=MQTTv311): """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 @@ -1713,8 +1713,8 @@ class Client(object): command = CONNECT packet = bytearray() packet.extend(struct.pack("!B", command)) - self._pack_remaining_length(packet, remaining_length) + self._pack_remaining_length(packet, remaining_length) packet.extend(struct.pack("!H"+str(len(protocol))+"sBBH", len(protocol), protocol, proto_ver, connect_flags, keepalive)) self._pack_str16(packet, self._client_id) @@ -1905,6 +1905,12 @@ class Client(object): return MQTT_ERR_PROTOCOL (resvd, result) = struct.unpack("!BB", self._in_packet['packet']) + if result == CONNACK_REFUSED_PROTOCOL_VERSION and self._protocol == MQTTv311: + self._easy_log(MQTT_LOG_DEBUG, "Received CONNACK ("+str(resvd)+", "+str(result)+"), attempting downgrade to MQTT v3.1.") + # Downgrade to MQTT v3.1 + self._protocol = MQTTv31 + return self.reconnect() + self._easy_log(MQTT_LOG_DEBUG, "Received CONNACK ("+str(resvd)+", "+str(result)+")") self._callback_mutex.acquire() if self.on_connect: diff --git a/src/paho/mqtt/publish.py b/src/paho/mqtt/publish.py index c1d1a26..89aae28 100644 --- a/src/paho/mqtt/publish.py +++ b/src/paho/mqtt/publish.py @@ -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, protocol=mqtt.MQTTv31): + will=None, auth=None, tls=None, protocol=mqtt.MQTTv311): """Publish multiple messages to a broker, then disconnect cleanly. This function creates an MQTT client, connects to a broker and publishes a @@ -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, protocol=mqtt.MQTTv31): + tls=None, protocol=mqtt.MQTTv311): """Publish a single message to a broker, then disconnect cleanly. This function creates an MQTT client, connects to a broker and publishes a diff --git a/test/paho_test.py b/test/paho_test.py index 111e524..a16c5d4 100644 --- a/test/paho_test.py +++ b/test/paho_test.py @@ -201,11 +201,11 @@ def to_string(packet): # Reserved return "0xF0" -def gen_connect(client_id, clean_session=True, keepalive=60, username=None, password=None, will_topic=None, will_qos=0, will_retain=False, will_payload="", proto_ver=3): +def gen_connect(client_id, clean_session=True, keepalive=60, username=None, password=None, will_topic=None, will_qos=0, will_retain=False, will_payload="", proto_name="MQTT", proto_ver=4): if client_id == None: remaining_length = 12 else: - remaining_length = 12 + 2+len(client_id) + remaining_length = 2+len(proto_name) + 1+1+2 + 2+len(client_id) connect_flags = 0 if clean_session: connect_flags = connect_flags | 0x02 @@ -225,7 +225,7 @@ def gen_connect(client_id, clean_session=True, keepalive=60, username=None, pass rl = pack_remaining_length(remaining_length) packet = struct.pack("!B"+str(len(rl))+"s", 0x10, rl) - packet = packet + struct.pack("!H6sBBH", len("MQIsdp"), "MQIsdp", proto_ver, connect_flags, keepalive) + packet = packet + struct.pack("!H"+str(len(proto_name))+"sBBH", len(proto_name), proto_name, proto_ver, connect_flags, keepalive) if client_id != None: packet = packet + struct.pack("!H"+str(len(client_id))+"s", len(client_id), client_id) -- 2.39.5