mirror of
https://github.com/espressif/esp-mqtt.git
synced 2025-07-31 03:08:03 +02:00
Fixed formatting for all files to comply with idf style formats
This commit is contained in:
0
include/mqtt_client.h
Executable file → Normal file
0
include/mqtt_client.h
Executable file → Normal file
@ -40,8 +40,7 @@ extern "C" {
|
|||||||
/* Remaining Length */
|
/* Remaining Length */
|
||||||
|
|
||||||
|
|
||||||
enum mqtt_message_type
|
enum mqtt_message_type {
|
||||||
{
|
|
||||||
MQTT_MSG_TYPE_CONNECT = 1,
|
MQTT_MSG_TYPE_CONNECT = 1,
|
||||||
MQTT_MSG_TYPE_CONNACK = 2,
|
MQTT_MSG_TYPE_CONNACK = 2,
|
||||||
MQTT_MSG_TYPE_PUBLISH = 3,
|
MQTT_MSG_TYPE_PUBLISH = 3,
|
||||||
@ -58,8 +57,7 @@ enum mqtt_message_type
|
|||||||
MQTT_MSG_TYPE_DISCONNECT = 14
|
MQTT_MSG_TYPE_DISCONNECT = 14
|
||||||
};
|
};
|
||||||
|
|
||||||
enum mqtt_connect_return_code
|
enum mqtt_connect_return_code {
|
||||||
{
|
|
||||||
CONNECTION_ACCEPTED = 0,
|
CONNECTION_ACCEPTED = 0,
|
||||||
CONNECTION_REFUSE_PROTOCOL,
|
CONNECTION_REFUSE_PROTOCOL,
|
||||||
CONNECTION_REFUSE_ID_REJECTED,
|
CONNECTION_REFUSE_ID_REJECTED,
|
||||||
@ -68,16 +66,14 @@ enum mqtt_connect_return_code
|
|||||||
CONNECTION_REFUSE_NOT_AUTHORIZED
|
CONNECTION_REFUSE_NOT_AUTHORIZED
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct mqtt_message
|
typedef struct mqtt_message {
|
||||||
{
|
|
||||||
uint8_t *data;
|
uint8_t *data;
|
||||||
uint32_t length;
|
uint32_t length;
|
||||||
uint32_t fragmented_msg_total_length; /*!< total len of fragmented messages (zero for all other messages) */
|
uint32_t fragmented_msg_total_length; /*!< total len of fragmented messages (zero for all other messages) */
|
||||||
uint32_t fragmented_msg_data_offset; /*!< data offset of fragmented messages (zero for all other messages) */
|
uint32_t fragmented_msg_data_offset; /*!< data offset of fragmented messages (zero for all other messages) */
|
||||||
} mqtt_message_t;
|
} mqtt_message_t;
|
||||||
|
|
||||||
typedef struct mqtt_connection
|
typedef struct mqtt_connection {
|
||||||
{
|
|
||||||
mqtt_message_t message;
|
mqtt_message_t message;
|
||||||
|
|
||||||
uint16_t message_id;
|
uint16_t message_id;
|
||||||
@ -86,8 +82,7 @@ typedef struct mqtt_connection
|
|||||||
|
|
||||||
} mqtt_connection_t;
|
} mqtt_connection_t;
|
||||||
|
|
||||||
typedef struct mqtt_connect_info
|
typedef struct mqtt_connect_info {
|
||||||
{
|
|
||||||
char *client_id;
|
char *client_id;
|
||||||
char *username;
|
char *username;
|
||||||
char *password;
|
char *password;
|
||||||
@ -102,13 +97,34 @@ typedef struct mqtt_connect_info
|
|||||||
} mqtt_connect_info_t;
|
} mqtt_connect_info_t;
|
||||||
|
|
||||||
|
|
||||||
static inline int mqtt_get_type(uint8_t* buffer) { return (buffer[0] & 0xf0) >> 4; }
|
static inline int mqtt_get_type(uint8_t *buffer)
|
||||||
static inline int mqtt_get_connect_session_present(uint8_t* buffer) { return buffer[2] & 0x01; }
|
{
|
||||||
static inline int mqtt_get_connect_return_code(uint8_t* buffer) { return buffer[3]; }
|
return (buffer[0] & 0xf0) >> 4;
|
||||||
static inline int mqtt_get_dup(uint8_t* buffer) { return (buffer[0] & 0x08) >> 3; }
|
}
|
||||||
static inline void mqtt_set_dup(uint8_t* buffer) { buffer[0] |= 0x08; }
|
static inline int mqtt_get_connect_session_present(uint8_t *buffer)
|
||||||
static inline int mqtt_get_qos(uint8_t* buffer) { return (buffer[0] & 0x06) >> 1; }
|
{
|
||||||
static inline int mqtt_get_retain(uint8_t* buffer) { return (buffer[0] & 0x01); }
|
return buffer[2] & 0x01;
|
||||||
|
}
|
||||||
|
static inline int mqtt_get_connect_return_code(uint8_t *buffer)
|
||||||
|
{
|
||||||
|
return buffer[3];
|
||||||
|
}
|
||||||
|
static inline int mqtt_get_dup(uint8_t *buffer)
|
||||||
|
{
|
||||||
|
return (buffer[0] & 0x08) >> 3;
|
||||||
|
}
|
||||||
|
static inline void mqtt_set_dup(uint8_t *buffer)
|
||||||
|
{
|
||||||
|
buffer[0] |= 0x08;
|
||||||
|
}
|
||||||
|
static inline int mqtt_get_qos(uint8_t *buffer)
|
||||||
|
{
|
||||||
|
return (buffer[0] & 0x06) >> 1;
|
||||||
|
}
|
||||||
|
static inline int mqtt_get_retain(uint8_t *buffer)
|
||||||
|
{
|
||||||
|
return (buffer[0] & 0x01);
|
||||||
|
}
|
||||||
|
|
||||||
void mqtt_msg_init(mqtt_connection_t *connection, uint8_t *buffer, uint16_t buffer_length);
|
void mqtt_msg_init(mqtt_connection_t *connection, uint8_t *buffer, uint16_t buffer_length);
|
||||||
bool mqtt_header_complete(uint8_t *buffer, uint16_t buffer_length);
|
bool mqtt_header_complete(uint8_t *buffer, uint16_t buffer_length);
|
||||||
|
206
lib/mqtt_msg.c
206
lib/mqtt_msg.c
@ -37,8 +37,7 @@
|
|||||||
|
|
||||||
#define MQTT_MAX_FIXED_HEADER_SIZE 5
|
#define MQTT_MAX_FIXED_HEADER_SIZE 5
|
||||||
|
|
||||||
enum mqtt_connect_flag
|
enum mqtt_connect_flag {
|
||||||
{
|
|
||||||
MQTT_CONNECT_FLAG_USERNAME = 1 << 7,
|
MQTT_CONNECT_FLAG_USERNAME = 1 << 7,
|
||||||
MQTT_CONNECT_FLAG_PASSWORD = 1 << 6,
|
MQTT_CONNECT_FLAG_PASSWORD = 1 << 6,
|
||||||
MQTT_CONNECT_FLAG_WILL_RETAIN = 1 << 5,
|
MQTT_CONNECT_FLAG_WILL_RETAIN = 1 << 5,
|
||||||
@ -46,8 +45,7 @@ enum mqtt_connect_flag
|
|||||||
MQTT_CONNECT_FLAG_CLEAN_SESSION = 1 << 1
|
MQTT_CONNECT_FLAG_CLEAN_SESSION = 1 << 1
|
||||||
};
|
};
|
||||||
|
|
||||||
struct __attribute((__packed__)) mqtt_connect_variable_header
|
struct __attribute((__packed__)) mqtt_connect_variable_header {
|
||||||
{
|
|
||||||
uint8_t lengthMsb;
|
uint8_t lengthMsb;
|
||||||
uint8_t lengthLsb;
|
uint8_t lengthLsb;
|
||||||
#if defined(MQTT_PROTOCOL_311)
|
#if defined(MQTT_PROTOCOL_311)
|
||||||
@ -63,8 +61,9 @@ struct __attribute((__packed__)) mqtt_connect_variable_header
|
|||||||
|
|
||||||
static int append_string(mqtt_connection_t *connection, const char *string, int len)
|
static int append_string(mqtt_connection_t *connection, const char *string, int len)
|
||||||
{
|
{
|
||||||
if (connection->message.length + len + 2 > connection->buffer_length)
|
if (connection->message.length + len + 2 > connection->buffer_length) {
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
connection->buffer[connection->message.length++] = len >> 8;
|
connection->buffer[connection->message.length++] = len >> 8;
|
||||||
connection->buffer[connection->message.length++] = len & 0xff;
|
connection->buffer[connection->message.length++] = len & 0xff;
|
||||||
@ -82,8 +81,9 @@ static uint16_t append_message_id(mqtt_connection_t* connection, uint16_t messag
|
|||||||
message_id = platform_random(65535);
|
message_id = platform_random(65535);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (connection->message.length + 2 > connection->buffer_length)
|
if (connection->message.length + 2 > connection->buffer_length) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
connection->buffer[connection->message.length++] = message_id >> 8;
|
connection->buffer[connection->message.length++] = message_id >> 8;
|
||||||
connection->buffer[connection->message.length++] = message_id & 0xff;
|
connection->buffer[connection->message.length++] = message_id & 0xff;
|
||||||
@ -159,17 +159,17 @@ uint32_t mqtt_get_total_length(uint8_t* buffer, uint16_t length, int* fixed_size
|
|||||||
int i;
|
int i;
|
||||||
uint32_t totlen = 0;
|
uint32_t totlen = 0;
|
||||||
|
|
||||||
for (i = 1; i < length; ++i)
|
for (i = 1; i < length; ++i) {
|
||||||
{
|
|
||||||
totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
|
totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
|
||||||
if ((buffer[i] & 0x80) == 0)
|
if ((buffer[i] & 0x80) == 0) {
|
||||||
{
|
|
||||||
++i;
|
++i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
totlen += i;
|
totlen += i;
|
||||||
if (fixed_size_len) *fixed_size_len = i;
|
if (fixed_size_len) {
|
||||||
|
*fixed_size_len = i;
|
||||||
|
}
|
||||||
|
|
||||||
return totlen;
|
return totlen;
|
||||||
}
|
}
|
||||||
@ -179,27 +179,26 @@ bool mqtt_header_complete(uint8_t* buffer, uint16_t buffer_length)
|
|||||||
uint16_t i;
|
uint16_t i;
|
||||||
uint16_t topiclen;
|
uint16_t topiclen;
|
||||||
|
|
||||||
for (i = 1; i < MQTT_MAX_FIXED_HEADER_SIZE; ++i)
|
for (i = 1; i < MQTT_MAX_FIXED_HEADER_SIZE; ++i) {
|
||||||
{
|
if (i >= buffer_length) {
|
||||||
if(i >= buffer_length)
|
|
||||||
return false;
|
return false;
|
||||||
if ((buffer[i] & 0x80) == 0)
|
}
|
||||||
{
|
if ((buffer[i] & 0x80) == 0) {
|
||||||
++i;
|
++i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// i is now the length of the fixed header
|
// i is now the length of the fixed header
|
||||||
|
|
||||||
if (i + 2 >= buffer_length)
|
if (i + 2 >= buffer_length) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
topiclen = buffer[i++] << 8;
|
topiclen = buffer[i++] << 8;
|
||||||
topiclen |= buffer[i++];
|
topiclen |= buffer[i++];
|
||||||
|
|
||||||
i += topiclen;
|
i += topiclen;
|
||||||
|
|
||||||
if (mqtt_get_qos(buffer) > 0)
|
if (mqtt_get_qos(buffer) > 0) {
|
||||||
{
|
|
||||||
i += 2;
|
i += 2;
|
||||||
}
|
}
|
||||||
// i is now the length of the fixed + variable header
|
// i is now the length of the fixed + variable header
|
||||||
@ -212,24 +211,24 @@ char* mqtt_get_publish_topic(uint8_t* buffer, uint32_t* length)
|
|||||||
int totlen = 0;
|
int totlen = 0;
|
||||||
int topiclen;
|
int topiclen;
|
||||||
|
|
||||||
for (i = 1; i < *length; ++i)
|
for (i = 1; i < *length; ++i) {
|
||||||
{
|
|
||||||
totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
|
totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
|
||||||
if ((buffer[i] & 0x80) == 0)
|
if ((buffer[i] & 0x80) == 0) {
|
||||||
{
|
|
||||||
++i;
|
++i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
totlen += i;
|
totlen += i;
|
||||||
|
|
||||||
if (i + 2 >= *length)
|
if (i + 2 >= *length) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
topiclen = buffer[i++] << 8;
|
topiclen = buffer[i++] << 8;
|
||||||
topiclen |= buffer[i++];
|
topiclen |= buffer[i++];
|
||||||
|
|
||||||
if (i + topiclen > *length)
|
if (i + topiclen > *length) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
*length = topiclen;
|
*length = topiclen;
|
||||||
return (char *)(buffer + i);
|
return (char *)(buffer + i);
|
||||||
@ -243,78 +242,79 @@ char* mqtt_get_publish_data(uint8_t* buffer, uint32_t* length)
|
|||||||
int blength = *length;
|
int blength = *length;
|
||||||
*length = 0;
|
*length = 0;
|
||||||
|
|
||||||
for (i = 1; i < blength; ++i)
|
for (i = 1; i < blength; ++i) {
|
||||||
{
|
|
||||||
totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
|
totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
|
||||||
if ((buffer[i] & 0x80) == 0)
|
if ((buffer[i] & 0x80) == 0) {
|
||||||
{
|
|
||||||
++i;
|
++i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
totlen += i;
|
totlen += i;
|
||||||
|
|
||||||
if (i + 2 >= blength)
|
if (i + 2 >= blength) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
topiclen = buffer[i++] << 8;
|
topiclen = buffer[i++] << 8;
|
||||||
topiclen |= buffer[i++];
|
topiclen |= buffer[i++];
|
||||||
|
|
||||||
if (i + topiclen >= blength)
|
if (i + topiclen >= blength) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
i += topiclen;
|
i += topiclen;
|
||||||
|
|
||||||
if (mqtt_get_qos(buffer) > 0)
|
if (mqtt_get_qos(buffer) > 0) {
|
||||||
{
|
if (i + 2 >= blength) {
|
||||||
if (i + 2 >= blength)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
i += 2;
|
i += 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (totlen < i)
|
if (totlen < i) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (totlen <= blength)
|
if (totlen <= blength) {
|
||||||
*length = totlen - i;
|
*length = totlen - i;
|
||||||
else
|
} else {
|
||||||
*length = blength - i;
|
*length = blength - i;
|
||||||
|
}
|
||||||
return (char *)(buffer + i);
|
return (char *)(buffer + i);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t mqtt_get_id(uint8_t *buffer, uint16_t length)
|
uint16_t mqtt_get_id(uint8_t *buffer, uint16_t length)
|
||||||
{
|
{
|
||||||
if (length < 1)
|
if (length < 1) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
switch (mqtt_get_type(buffer))
|
switch (mqtt_get_type(buffer)) {
|
||||||
{
|
case MQTT_MSG_TYPE_PUBLISH: {
|
||||||
case MQTT_MSG_TYPE_PUBLISH:
|
|
||||||
{
|
|
||||||
int i;
|
int i;
|
||||||
int topiclen;
|
int topiclen;
|
||||||
|
|
||||||
for (i = 1; i < length; ++i)
|
for (i = 1; i < length; ++i) {
|
||||||
{
|
if ((buffer[i] & 0x80) == 0) {
|
||||||
if ((buffer[i] & 0x80) == 0)
|
|
||||||
{
|
|
||||||
++i;
|
++i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i + 2 >= length)
|
if (i + 2 >= length) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
topiclen = buffer[i++] << 8;
|
topiclen = buffer[i++] << 8;
|
||||||
topiclen |= buffer[i++];
|
topiclen |= buffer[i++];
|
||||||
|
|
||||||
if (i + topiclen > length)
|
if (i + topiclen > length) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
i += topiclen;
|
i += topiclen;
|
||||||
|
|
||||||
if (mqtt_get_qos(buffer) > 0)
|
if (mqtt_get_qos(buffer) > 0) {
|
||||||
{
|
if (i + 2 > length) {
|
||||||
if (i + 2 > length)
|
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
//i += 2;
|
//i += 2;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
@ -329,15 +329,15 @@ uint16_t mqtt_get_id(uint8_t* buffer, uint16_t length)
|
|||||||
case MQTT_MSG_TYPE_SUBACK:
|
case MQTT_MSG_TYPE_SUBACK:
|
||||||
case MQTT_MSG_TYPE_UNSUBACK:
|
case MQTT_MSG_TYPE_UNSUBACK:
|
||||||
case MQTT_MSG_TYPE_SUBSCRIBE:
|
case MQTT_MSG_TYPE_SUBSCRIBE:
|
||||||
case MQTT_MSG_TYPE_UNSUBSCRIBE:
|
case MQTT_MSG_TYPE_UNSUBSCRIBE: {
|
||||||
{
|
|
||||||
// This requires the remaining length to be encoded in 1 byte,
|
// This requires the remaining length to be encoded in 1 byte,
|
||||||
// which it should be.
|
// which it should be.
|
||||||
if (length >= 4 && (buffer[1] & 0x80) == 0)
|
if (length >= 4 && (buffer[1] & 0x80) == 0) {
|
||||||
return (buffer[2] << 8) | buffer[3];
|
return (buffer[2] << 8) | buffer[3];
|
||||||
else
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
@ -350,8 +350,9 @@ mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_inf
|
|||||||
|
|
||||||
init_message(connection);
|
init_message(connection);
|
||||||
|
|
||||||
if (connection->message.length + sizeof(*variable_header) > connection->buffer_length)
|
if (connection->message.length + sizeof(*variable_header) > connection->buffer_length) {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
variable_header = (void *)(connection->buffer + connection->message.length);
|
variable_header = (void *)(connection->buffer + connection->message.length);
|
||||||
connection->message.length += sizeof(*variable_header);
|
connection->message.length += sizeof(*variable_header);
|
||||||
|
|
||||||
@ -370,43 +371,46 @@ mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_inf
|
|||||||
variable_header->keepaliveMsb = info->keepalive >> 8;
|
variable_header->keepaliveMsb = info->keepalive >> 8;
|
||||||
variable_header->keepaliveLsb = info->keepalive & 0xff;
|
variable_header->keepaliveLsb = info->keepalive & 0xff;
|
||||||
|
|
||||||
if (info->clean_session)
|
if (info->clean_session) {
|
||||||
variable_header->flags |= MQTT_CONNECT_FLAG_CLEAN_SESSION;
|
variable_header->flags |= MQTT_CONNECT_FLAG_CLEAN_SESSION;
|
||||||
|
}
|
||||||
|
|
||||||
if (info->client_id != NULL && info->client_id[0] != '\0')
|
if (info->client_id != NULL && info->client_id[0] != '\0') {
|
||||||
{
|
if (append_string(connection, info->client_id, strlen(info->client_id)) < 0) {
|
||||||
if (append_string(connection, info->client_id, strlen(info->client_id)) < 0)
|
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
}
|
}
|
||||||
else
|
} else {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
|
||||||
if (info->will_topic != NULL && info->will_topic[0] != '\0')
|
if (info->will_topic != NULL && info->will_topic[0] != '\0') {
|
||||||
{
|
if (append_string(connection, info->will_topic, strlen(info->will_topic)) < 0) {
|
||||||
if (append_string(connection, info->will_topic, strlen(info->will_topic)) < 0)
|
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
|
||||||
if (append_string(connection, info->will_message, info->will_length) < 0)
|
if (append_string(connection, info->will_message, info->will_length) < 0) {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
|
||||||
variable_header->flags |= MQTT_CONNECT_FLAG_WILL;
|
variable_header->flags |= MQTT_CONNECT_FLAG_WILL;
|
||||||
if (info->will_retain)
|
if (info->will_retain) {
|
||||||
variable_header->flags |= MQTT_CONNECT_FLAG_WILL_RETAIN;
|
variable_header->flags |= MQTT_CONNECT_FLAG_WILL_RETAIN;
|
||||||
|
}
|
||||||
variable_header->flags |= (info->will_qos & 3) << 3;
|
variable_header->flags |= (info->will_qos & 3) << 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (info->username != NULL && info->username[0] != '\0')
|
if (info->username != NULL && info->username[0] != '\0') {
|
||||||
{
|
if (append_string(connection, info->username, strlen(info->username)) < 0) {
|
||||||
if (append_string(connection, info->username, strlen(info->username)) < 0)
|
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
|
||||||
variable_header->flags |= MQTT_CONNECT_FLAG_USERNAME;
|
variable_header->flags |= MQTT_CONNECT_FLAG_USERNAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (info->password != NULL && info->password[0] != '\0')
|
if (info->password != NULL && info->password[0] != '\0') {
|
||||||
{
|
if (append_string(connection, info->password, strlen(info->password)) < 0) {
|
||||||
if (append_string(connection, info->password, strlen(info->password)) < 0)
|
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
|
||||||
variable_header->flags |= MQTT_CONNECT_FLAG_PASSWORD;
|
variable_header->flags |= MQTT_CONNECT_FLAG_PASSWORD;
|
||||||
}
|
}
|
||||||
@ -418,19 +422,21 @@ mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topi
|
|||||||
{
|
{
|
||||||
init_message(connection);
|
init_message(connection);
|
||||||
|
|
||||||
if (topic == NULL || topic[0] == '\0')
|
if (topic == NULL || topic[0] == '\0') {
|
||||||
return fail_message(connection);
|
|
||||||
|
|
||||||
if (append_string(connection, topic, strlen(topic)) < 0)
|
|
||||||
return fail_message(connection);
|
|
||||||
|
|
||||||
if (qos > 0)
|
|
||||||
{
|
|
||||||
if ((*message_id = append_message_id(connection, 0)) == 0)
|
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
if (append_string(connection, topic, strlen(topic)) < 0) {
|
||||||
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (qos > 0) {
|
||||||
|
if ((*message_id = append_message_id(connection, 0)) == 0) {
|
||||||
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
*message_id = 0;
|
*message_id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (connection->message.length + data_length > connection->buffer_length) {
|
if (connection->message.length + data_length > connection->buffer_length) {
|
||||||
// Not enough size in buffer -> fragment this message
|
// Not enough size in buffer -> fragment this message
|
||||||
@ -449,32 +455,36 @@ mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topi
|
|||||||
mqtt_message_t *mqtt_msg_puback(mqtt_connection_t *connection, uint16_t message_id)
|
mqtt_message_t *mqtt_msg_puback(mqtt_connection_t *connection, uint16_t message_id)
|
||||||
{
|
{
|
||||||
init_message(connection);
|
init_message(connection);
|
||||||
if (append_message_id(connection, message_id) == 0)
|
if (append_message_id(connection, message_id) == 0) {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
return fini_message(connection, MQTT_MSG_TYPE_PUBACK, 0, 0, 0);
|
return fini_message(connection, MQTT_MSG_TYPE_PUBACK, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
mqtt_message_t *mqtt_msg_pubrec(mqtt_connection_t *connection, uint16_t message_id)
|
mqtt_message_t *mqtt_msg_pubrec(mqtt_connection_t *connection, uint16_t message_id)
|
||||||
{
|
{
|
||||||
init_message(connection);
|
init_message(connection);
|
||||||
if (append_message_id(connection, message_id) == 0)
|
if (append_message_id(connection, message_id) == 0) {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
return fini_message(connection, MQTT_MSG_TYPE_PUBREC, 0, 0, 0);
|
return fini_message(connection, MQTT_MSG_TYPE_PUBREC, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
mqtt_message_t *mqtt_msg_pubrel(mqtt_connection_t *connection, uint16_t message_id)
|
mqtt_message_t *mqtt_msg_pubrel(mqtt_connection_t *connection, uint16_t message_id)
|
||||||
{
|
{
|
||||||
init_message(connection);
|
init_message(connection);
|
||||||
if (append_message_id(connection, message_id) == 0)
|
if (append_message_id(connection, message_id) == 0) {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
return fini_message(connection, MQTT_MSG_TYPE_PUBREL, 0, 1, 0);
|
return fini_message(connection, MQTT_MSG_TYPE_PUBREL, 0, 1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
mqtt_message_t *mqtt_msg_pubcomp(mqtt_connection_t *connection, uint16_t message_id)
|
mqtt_message_t *mqtt_msg_pubcomp(mqtt_connection_t *connection, uint16_t message_id)
|
||||||
{
|
{
|
||||||
init_message(connection);
|
init_message(connection);
|
||||||
if (append_message_id(connection, message_id) == 0)
|
if (append_message_id(connection, message_id) == 0) {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
return fini_message(connection, MQTT_MSG_TYPE_PUBCOMP, 0, 0, 0);
|
return fini_message(connection, MQTT_MSG_TYPE_PUBCOMP, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -482,17 +492,21 @@ mqtt_message_t* mqtt_msg_subscribe(mqtt_connection_t* connection, const char* to
|
|||||||
{
|
{
|
||||||
init_message(connection);
|
init_message(connection);
|
||||||
|
|
||||||
if (topic == NULL || topic[0] == '\0')
|
if (topic == NULL || topic[0] == '\0') {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
|
||||||
if ((*message_id = append_message_id(connection, 0)) == 0)
|
if ((*message_id = append_message_id(connection, 0)) == 0) {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
|
||||||
if (append_string(connection, topic, strlen(topic)) < 0)
|
if (append_string(connection, topic, strlen(topic)) < 0) {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
|
||||||
if (connection->message.length + 1 > connection->buffer_length)
|
if (connection->message.length + 1 > connection->buffer_length) {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
connection->buffer[connection->message.length++] = qos;
|
connection->buffer[connection->message.length++] = qos;
|
||||||
|
|
||||||
return fini_message(connection, MQTT_MSG_TYPE_SUBSCRIBE, 0, 1, 0);
|
return fini_message(connection, MQTT_MSG_TYPE_SUBSCRIBE, 0, 1, 0);
|
||||||
@ -502,14 +516,17 @@ mqtt_message_t* mqtt_msg_unsubscribe(mqtt_connection_t* connection, const char*
|
|||||||
{
|
{
|
||||||
init_message(connection);
|
init_message(connection);
|
||||||
|
|
||||||
if (topic == NULL || topic[0] == '\0')
|
if (topic == NULL || topic[0] == '\0') {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
|
||||||
if ((*message_id = append_message_id(connection, 0)) == 0)
|
if ((*message_id = append_message_id(connection, 0)) == 0) {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
|
||||||
if (append_string(connection, topic, strlen(topic)) < 0)
|
if (append_string(connection, topic, strlen(topic)) < 0) {
|
||||||
return fail_message(connection);
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
|
||||||
return fini_message(connection, MQTT_MSG_TYPE_UNSUBSCRIBE, 0, 1, 0);
|
return fini_message(connection, MQTT_MSG_TYPE_UNSUBSCRIBE, 0, 1, 0);
|
||||||
}
|
}
|
||||||
@ -543,8 +560,7 @@ int mqtt_has_valid_msg_hdr(uint8_t* buffer, uint16_t length)
|
|||||||
if (length < 1) {
|
if (length < 1) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
switch (mqtt_get_type(buffer))
|
switch (mqtt_get_type(buffer)) {
|
||||||
{
|
|
||||||
case MQTT_MSG_TYPE_CONNECT:
|
case MQTT_MSG_TYPE_CONNECT:
|
||||||
case MQTT_MSG_TYPE_CONNACK:
|
case MQTT_MSG_TYPE_CONNACK:
|
||||||
case MQTT_MSG_TYPE_PUBACK:
|
case MQTT_MSG_TYPE_PUBACK:
|
||||||
|
@ -27,8 +27,7 @@
|
|||||||
|
|
||||||
static const char *TAG = "MQTT_CLIENT";
|
static const char *TAG = "MQTT_CLIENT";
|
||||||
|
|
||||||
typedef struct mqtt_state
|
typedef struct mqtt_state {
|
||||||
{
|
|
||||||
mqtt_connect_info_t *connect_info;
|
mqtt_connect_info_t *connect_info;
|
||||||
uint8_t *in_buffer;
|
uint8_t *in_buffer;
|
||||||
uint8_t *out_buffer;
|
uint8_t *out_buffer;
|
||||||
@ -822,8 +821,7 @@ static esp_err_t mqtt_process_receive(esp_mqtt_client_handle_t client)
|
|||||||
|
|
||||||
ESP_LOGD(TAG, "msg_type=%d, msg_id=%d", msg_type, msg_id);
|
ESP_LOGD(TAG, "msg_type=%d, msg_id=%d", msg_type, msg_id);
|
||||||
|
|
||||||
switch (msg_type)
|
switch (msg_type) {
|
||||||
{
|
|
||||||
case MQTT_MSG_TYPE_SUBACK:
|
case MQTT_MSG_TYPE_SUBACK:
|
||||||
if (is_valid_mqtt_msg(client, MQTT_MSG_TYPE_SUBSCRIBE, msg_id)) {
|
if (is_valid_mqtt_msg(client, MQTT_MSG_TYPE_SUBSCRIBE, msg_id)) {
|
||||||
ESP_LOGD(TAG, "Subscribe successful");
|
ESP_LOGD(TAG, "Subscribe successful");
|
||||||
@ -846,8 +844,7 @@ static esp_err_t mqtt_process_receive(esp_mqtt_client_handle_t client)
|
|||||||
}
|
}
|
||||||
if (msg_qos == 1) {
|
if (msg_qos == 1) {
|
||||||
client->mqtt_state.outbound_message = mqtt_msg_puback(&client->mqtt_state.mqtt_connection, msg_id);
|
client->mqtt_state.outbound_message = mqtt_msg_puback(&client->mqtt_state.mqtt_connection, msg_id);
|
||||||
}
|
} else if (msg_qos == 2) {
|
||||||
else if (msg_qos == 2) {
|
|
||||||
client->mqtt_state.outbound_message = mqtt_msg_pubrec(&client->mqtt_state.mqtt_connection, msg_id);
|
client->mqtt_state.outbound_message = mqtt_msg_pubrec(&client->mqtt_state.mqtt_connection, msg_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user