34 #include "user_config.h" 35 #define MQTT_MAX_FIXED_HEADER_SIZE 3 50 #if defined(PROTOCOL_NAMEv31) 52 #elif defined(PROTOCOL_NAMEv311) 55 #error "Please define protocol name" 80 while (message_id == 0)
109 if (remaining_length > 127)
111 connection->
buffer[0] = ((type & 0x0f) << 4) | ((dup & 1) << 3) | ((qos & 3) << 1) | (retain & 1);
112 connection->
buffer[1] = 0x80 | (remaining_length % 128);
113 connection->
buffer[2] = remaining_length / 128;
119 connection->
buffer[1] = ((type & 0x0f) << 4) | ((dup & 1) << 3) | ((qos & 3) << 1) | (retain & 1);
120 connection->
buffer[2] = remaining_length;
131 connection->
buffer = buffer;
140 for (i = 1; i < length; ++i)
142 totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
143 if ((buffer[i] & 0x80) == 0)
160 for (i = 1; i < *length; ++i)
162 totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
163 if ((buffer[i] & 0x80) == 0)
171 if (i + 2 >= *length)
173 topiclen = buffer[i++] << 8;
174 topiclen |= buffer[i++];
176 if (i + topiclen > *length)
180 return (
const char*)(buffer + i);
188 int blength = *length;
191 for (i = 1; i < blength; ++i)
193 totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
194 if ((buffer[i] & 0x80) == 0)
202 if (i + 2 >= blength)
204 topiclen = buffer[i++] << 8;
205 topiclen |= buffer[i++];
207 if (i + topiclen >= blength)
214 if (i + 2 >= blength)
222 if (totlen <= blength)
223 *length = totlen - i;
225 *length = blength - i;
226 return (
const char*)(buffer + i);
241 for (i = 1; i < length; ++i)
243 if ((buffer[i] & 0x80) == 0)
252 topiclen = buffer[i++] << 8;
253 topiclen |= buffer[i++];
255 if (i + topiclen >= length)
268 return (buffer[i] << 8) | buffer[i + 1];
280 if (length >= 4 && (buffer[1] & 0x80) == 0)
281 return (buffer[2] << 8) | buffer[3];
293 struct mqtt_connect_variable_header* variable_header;
302 variable_header->lengthMsb = 0;
303 #if defined(PROTOCOL_NAMEv31) 304 variable_header->lengthLsb = 6;
305 memcpy(variable_header->magic,
"MQIsdp", 6);
306 variable_header->version = 3;
307 #elif defined(PROTOCOL_NAMEv311) 308 variable_header->lengthLsb = 4;
309 memcpy(variable_header->magic,
"MQTT", 4);
310 variable_header->version = 4;
312 #error "Please define protocol name" 315 variable_header->flags = 0;
316 variable_header->keepaliveMsb = info->
keepalive >> 8;
317 variable_header->keepaliveLsb = info->
keepalive & 0xff;
329 #ifdef PROTOCOL_NAMEv311 355 variable_header->flags |= (info->
will_qos & 3) << 3;
381 if (topic ==
NULL || topic[0] ==
'\0')
439 if (topic ==
NULL || topic[0] ==
'\0')
459 if (topic ==
NULL || topic[0] ==
'\0')
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_unsubscribe(mqtt_connection_t *connection, const char *topic, uint16_t *message_id)
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_connect(mqtt_connection_t *connection, mqtt_connect_info_t *info)
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_puback(mqtt_connection_t *connection, uint16_t message_id)
static mqtt_message_t *ICACHE_FLASH_ATTR fini_message(mqtt_connection_t *connection, int type, int dup, int qos, int retain)
const char *ICACHE_FLASH_ATTR mqtt_get_publish_topic(uint8_t *buffer, uint16_t *length)
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_disconnect(mqtt_connection_t *connection)
static mqtt_message_t *ICACHE_FLASH_ATTR fail_message(mqtt_connection_t *connection)
uint16_t ICACHE_FLASH_ATTR mqtt_get_id(uint8_t *buffer, uint16_t length)
struct __attribute((__packed__))
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_publish(mqtt_connection_t *connection, const char *topic, const char *data, int data_length, int qos, int retain, uint16_t *message_id)
static uint16_t ICACHE_FLASH_ATTR append_message_id(mqtt_connection_t *connection, uint16_t message_id)
#define ICACHE_FLASH_ATTR
void ICACHE_FLASH_ATTR mqtt_msg_init(mqtt_connection_t *connection, uint8_t *buffer, uint16_t buffer_length)
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_pingreq(mqtt_connection_t *connection)
static int ICACHE_FLASH_ATTR append_string(mqtt_connection_t *connection, const char *string, int len)
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_pingresp(mqtt_connection_t *connection)
static int ICACHE_FLASH_ATTR init_message(mqtt_connection_t *connection)
#define MQTT_MAX_FIXED_HEADER_SIZE
const char *ICACHE_FLASH_ATTR mqtt_get_publish_data(uint8_t *buffer, uint16_t *length)
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_pubcomp(mqtt_connection_t *connection, uint16_t message_id)
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_pubrec(mqtt_connection_t *connection, uint16_t message_id)
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_pubrel(mqtt_connection_t *connection, uint16_t message_id)
int ICACHE_FLASH_ATTR mqtt_get_total_length(uint8_t *buffer, uint16_t length)
static int ICACHE_FLASH_ATTR mqtt_get_type(uint8_t *buffer)
static int ICACHE_FLASH_ATTR mqtt_get_qos(uint8_t *buffer)
mqtt_message_t *ICACHE_FLASH_ATTR mqtt_msg_subscribe(mqtt_connection_t *connection, const char *topic, int qos, uint16_t *message_id)