Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-negotiate MQTT version, rather than using a #define. #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions Adafruit_MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server,
will_retain = 0;

packet_id_counter = 0;
mqtt_protocol_level = 4;

}

Expand All @@ -159,7 +160,11 @@ int8_t Adafruit_MQTT::connect() {
// Read connect response packet and verify it
len = readFullPacket(buffer, MAXBUFFERSIZE, CONNECT_TIMEOUT_MS);
if (len != 4)
return -1;
if(mqtt_protocol_level == 4) {
mqtt_protocol_level = 3;
return connect();
} else
return -1;
if ((buffer[0] != (MQTT_CTRL_CONNECTACK << 4)) || (buffer[1] != 2))
return -1;
if (buffer[3] != 0)
Expand All @@ -177,7 +182,7 @@ int8_t Adafruit_MQTT::connect() {
if (!sendPacket(buffer, len))
return -1;

if(MQTT_PROTOCOL_LEVEL < 3) // older versions didn't suback
if(mqtt_protocol_level < 3) // older versions didn't suback
break;

// Check for SUBACK if using MQTT 3.1.1 or higher
Expand Down Expand Up @@ -383,7 +388,7 @@ bool Adafruit_MQTT::unsubscribe(Adafruit_MQTT_Subscribe *sub) {

// if QoS for this subscription is 1 or 2, we need
// to wait for the unsuback to confirm unsubscription
if(subscriptions[i]->qos > 0 && MQTT_PROTOCOL_LEVEL > 3) {
if(subscriptions[i]->qos > 0 && mqtt_protocol_level > 3) {

// wait for UNSUBACK
len = readFullPacket(buffer, MAXBUFFERSIZE, CONNECT_TIMEOUT_MS);
Expand Down Expand Up @@ -504,7 +509,7 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
DEBUG_PRINT(F("Data len: ")); DEBUG_PRINTLN(datalen);
DEBUG_PRINT(F("Data: ")); DEBUG_PRINTLN((char *)subscriptions[i]->lastread);

if ((MQTT_PROTOCOL_LEVEL > 3) &&(buffer[0] & 0x6) == 0x2) {
if ((mqtt_protocol_level > 3) &&(buffer[0] & 0x6) == 0x2) {
uint8_t ackpacket[4];

// Construct and send puback packet.
Expand Down Expand Up @@ -557,15 +562,13 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
p+=2;
// fill in packet[1] last

#if MQTT_PROTOCOL_LEVEL == 3
if(mqtt_protocol_level == 3) {
p = stringprint(p, "MQIsdp");
#elif MQTT_PROTOCOL_LEVEL == 4
} else {
p = stringprint(p, "MQTT");
#else
#error "MQTT level not supported"
#endif
}

p[0] = MQTT_PROTOCOL_LEVEL;
p[0] = mqtt_protocol_level;
p++;

// always clean the session
Expand Down Expand Up @@ -597,7 +600,7 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
p[0] = MQTT_CONN_KEEPALIVE & 0xFF;
p++;

if(MQTT_PROTOCOL_LEVEL == 3) {
if(mqtt_protocol_level == 3) {
p = stringprint(p, clientid, 23); // Limit client ID to first 23 characters.
} else {
if (pgm_read_byte(clientid) != 0) {
Expand Down
4 changes: 1 addition & 3 deletions Adafruit_MQTT.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@
#define ERROR_PRINTBUFFER(buffer, len) {}
#endif

// Use 3 (MQTT 3.0) or 4 (MQTT 3.1.1)
#define MQTT_PROTOCOL_LEVEL 4

#define MQTT_CTRL_CONNECT 0x1
#define MQTT_CTRL_CONNECTACK 0x2
#define MQTT_CTRL_PUBLISH 0x3
Expand Down Expand Up @@ -235,6 +232,7 @@ class Adafruit_MQTT {
uint8_t will_retain;
uint8_t buffer[MAXBUFFERSIZE]; // one buffer, used for all incoming/outgoing
uint16_t packet_id_counter;
uint8_t mqtt_protocol_level;

private:
Adafruit_MQTT_Subscribe *subscriptions[MAXSUBSCRIPTIONS];
Expand Down