From 774f5a99d4ca7f4ea2cd4d445247cf9c1590a80e Mon Sep 17 00:00:00 2001 From: Tuan PM Date: Fri, 6 Feb 2015 09:41:37 +0700 Subject: [PATCH] add protocol name selection(3.11 or 3.1), defaut is v3.1, resolve #36 --- include/user_config.h | 2 ++ mqtt/mqtt_msg.c | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/user_config.h b/include/user_config.h index 106023c..b7694a8 100644 --- a/include/user_config.h +++ b/include/user_config.h @@ -25,4 +25,6 @@ #define DEFAULT_SECURITY 0 #define QUEUE_BUFFER_SIZE 2048 +#define PROTOCOL_NAMEv31 /*MQTT version 3.1 compatible with Mosquitto v0.15*/ +//PROTOCOL_NAMEv311 /*MQTT version 3.11 compatible with https://eclipse.org/paho/clients/testing/*/ #endif diff --git a/mqtt/mqtt_msg.c b/mqtt/mqtt_msg.c index 460a625..67d4425 100644 --- a/mqtt/mqtt_msg.c +++ b/mqtt/mqtt_msg.c @@ -31,7 +31,7 @@ #include #include "mqtt_msg.h" - +#include "user_config.h" #define MQTT_MAX_FIXED_HEADER_SIZE 3 enum mqtt_connect_flag @@ -47,7 +47,13 @@ struct __attribute((__packed__)) mqtt_connect_variable_header { uint8_t lengthMsb; uint8_t lengthLsb; +#if defined(PROTOCOL_NAMEv31) + uint8_t magic[6]; +#elif defined(PROTOCOL_NAMEv311) uint8_t magic[4]; +#else +#error "Please define protocol name" +#endif uint8_t version; uint8_t flags; uint8_t keepaliveMsb; @@ -293,9 +299,18 @@ mqtt_message_t* ICACHE_FLASH_ATTR mqtt_msg_connect(mqtt_connection_t* connection connection->message.length += sizeof(*variable_header); variable_header->lengthMsb = 0; +#if defined(PROTOCOL_NAMEv31) + variable_header->lengthLsb = 6; + memcpy(variable_header->magic, "MQIsdp", 6); + variable_header->version = 3; +#elif defined(PROTOCOL_NAMEv311) variable_header->lengthLsb = 4; memcpy(variable_header->magic, "MQTT", 4); variable_header->version = 4; +#else +#error "Please define protocol name" +#endif + variable_header->flags = 0; variable_header->keepaliveMsb = info->keepalive >> 8; variable_header->keepaliveLsb = info->keepalive & 0xff;