| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | /*
 | 
					
						
							|  |  |  | * Copyright (c) 2014, Stephen Robinson | 
					
						
							|  |  |  | * All rights reserved. | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  | * modification, are permitted provided that the following conditions | 
					
						
							|  |  |  | * are met: | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * 1. Redistributions of source code must retain the above copyright | 
					
						
							|  |  |  | * notice, this list of conditions and the following disclaimer. | 
					
						
							|  |  |  | * 2. Redistributions in binary form must reproduce the above copyright | 
					
						
							|  |  |  | * notice, this list of conditions and the following disclaimer in the | 
					
						
							|  |  |  | * documentation and/or other materials provided with the distribution. | 
					
						
							|  |  |  | * 3. Neither the name of the copyright holder nor the names of its | 
					
						
							|  |  |  | * contributors may be used to endorse or promote products derived | 
					
						
							|  |  |  | * from this software without specific prior written permission. | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
					
						
							|  |  |  | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | 
					
						
							|  |  |  | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 
					
						
							|  |  |  | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 
					
						
							|  |  |  | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 
					
						
							|  |  |  | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 
					
						
							|  |  |  | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 
					
						
							|  |  |  | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 
					
						
							|  |  |  | * POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2022-12-12 15:27:40 +01:00
										 |  |  | #include "mqtt_client.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | #include "mqtt_msg.h"
 | 
					
						
							|  |  |  | #include "mqtt_config.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-16 02:40:16 +07:00
										 |  |  | #include "platform.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-25 16:38:25 +02:00
										 |  |  | #define MQTT_MAX_FIXED_HEADER_SIZE 5
 | 
					
						
							| 
									
										
										
										
											2020-01-09 11:50:40 +08:00
										 |  |  | #define MQTT_3_1_VARIABLE_HEADER_SIZE 12
 | 
					
						
							|  |  |  | #define MQTT_3_1_1_VARIABLE_HEADER_SIZE 10
 | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | enum mqtt_connect_flag { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     MQTT_CONNECT_FLAG_USERNAME = 1 << 7, | 
					
						
							|  |  |  |     MQTT_CONNECT_FLAG_PASSWORD = 1 << 6, | 
					
						
							|  |  |  |     MQTT_CONNECT_FLAG_WILL_RETAIN = 1 << 5, | 
					
						
							|  |  |  |     MQTT_CONNECT_FLAG_WILL = 1 << 2, | 
					
						
							|  |  |  |     MQTT_CONNECT_FLAG_CLEAN_SESSION = 1 << 1 | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | static int append_string(mqtt_connection_t *connection, const char *string, int len) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     if (connection->outbound_message.length + len + 2 > connection->buffer_length) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     connection->buffer[connection->outbound_message.length++] = len >> 8; | 
					
						
							|  |  |  |     connection->buffer[connection->outbound_message.length++] = len & 0xff; | 
					
						
							|  |  |  |     memcpy(connection->buffer + connection->outbound_message.length, string, len); | 
					
						
							|  |  |  |     connection->outbound_message.length += len; | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return len + 2; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | static uint16_t append_message_id(mqtt_connection_t *connection, uint16_t message_id) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							|  |  |  |     // If message_id is zero then we should assign one, otherwise
 | 
					
						
							|  |  |  |     // we'll use the one supplied by the caller
 | 
					
						
							| 
									
										
										
										
											2018-02-16 02:40:16 +07:00
										 |  |  |     while (message_id == 0) { | 
					
						
							| 
									
										
										
										
											2020-12-06 11:46:36 +01:00
										 |  |  | #if MQTT_MSG_ID_INCREMENTAL
 | 
					
						
							|  |  |  |         message_id = ++connection->last_message_id; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2018-02-16 02:40:16 +07:00
										 |  |  |         message_id = platform_random(65535); | 
					
						
							| 
									
										
										
										
											2020-12-06 11:46:36 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-02-16 02:40:16 +07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     if (connection->outbound_message.length + 2 > connection->buffer_length) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     connection->buffer[connection->outbound_message.length++] = message_id >> 8; | 
					
						
							|  |  |  |     connection->buffer[connection->outbound_message.length++] = message_id & 0xff; | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return message_id; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  | static int set_message_header_size(mqtt_connection_t *connection) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     connection->outbound_message.length = MQTT_MAX_FIXED_HEADER_SIZE; | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     return MQTT_MAX_FIXED_HEADER_SIZE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | static mqtt_message_t *fail_message(mqtt_connection_t *connection) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     connection->outbound_message.data = connection->buffer; | 
					
						
							|  |  |  |     connection->outbound_message.length = 0; | 
					
						
							|  |  |  |     return &connection->outbound_message; | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | static mqtt_message_t *fini_message(mqtt_connection_t *connection, int type, int dup, int qos, int retain) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     int message_length = connection->outbound_message.length - MQTT_MAX_FIXED_HEADER_SIZE; | 
					
						
							| 
									
										
										
										
											2018-10-25 16:38:25 +02:00
										 |  |  |     int total_length = message_length; | 
					
						
							|  |  |  |     int encoded_length = 0; | 
					
						
							|  |  |  |     uint8_t encoded_lens[4] = {0}; | 
					
						
							|  |  |  |     // Check if we have fragmented message and update total_len
 | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     if (connection->outbound_message.fragmented_msg_total_length) { | 
					
						
							|  |  |  |         total_length = connection->outbound_message.fragmented_msg_total_length - MQTT_MAX_FIXED_HEADER_SIZE; | 
					
						
							| 
									
										
										
										
											2018-10-25 16:38:25 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-25 16:38:25 +02:00
										 |  |  |     // Encode MQTT message length
 | 
					
						
							|  |  |  |     int len_bytes = 0; // size of encoded message length
 | 
					
						
							|  |  |  |     do { | 
					
						
							|  |  |  |         encoded_length = total_length % 128; | 
					
						
							|  |  |  |         total_length /= 128; | 
					
						
							|  |  |  |         if (total_length > 0) { | 
					
						
							|  |  |  |             encoded_length |= 0x80; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         encoded_lens[len_bytes] = encoded_length; | 
					
						
							|  |  |  |         len_bytes++; | 
					
						
							|  |  |  |     } while (total_length > 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Sanity check for MQTT header
 | 
					
						
							|  |  |  |     if (len_bytes + 1 > MQTT_MAX_FIXED_HEADER_SIZE) { | 
					
						
							|  |  |  |         return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-25 16:38:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Save the header bytes
 | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     connection->outbound_message.length = message_length + len_bytes + 1; // msg len + encoded_size len + type (1 byte)
 | 
					
						
							| 
									
										
										
										
											2018-10-25 16:38:25 +02:00
										 |  |  |     int offs = MQTT_MAX_FIXED_HEADER_SIZE - 1 - len_bytes; | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     connection->outbound_message.data = connection->buffer + offs; | 
					
						
							|  |  |  |     connection->outbound_message.fragmented_msg_data_offset -= offs; | 
					
						
							| 
									
										
										
										
											2018-10-25 16:38:25 +02:00
										 |  |  |     // type byte
 | 
					
						
							|  |  |  |     connection->buffer[offs++] =  ((type & 0x0f) << 4) | ((dup & 1) << 3) | ((qos & 3) << 1) | (retain & 1); | 
					
						
							|  |  |  |     // length bytes
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     for (int j = 0; j < len_bytes; j++) { | 
					
						
							| 
									
										
										
										
											2018-10-25 16:38:25 +02:00
										 |  |  |         connection->buffer[offs++] = encoded_lens[j]; | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     return &connection->outbound_message; | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 16:33:12 +11:00
										 |  |  | size_t mqtt_get_total_length(const uint8_t *buffer, size_t length, int *fixed_size_len) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							|  |  |  |     int i; | 
					
						
							| 
									
										
										
										
											2020-10-09 16:33:12 +11:00
										 |  |  |     size_t totlen = 0; | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     for (i = 1; i < length; ++i) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         totlen += (buffer[i] & 0x7f) << (7 * (i - 1)); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         if ((buffer[i] & 0x80) == 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |             ++i; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     totlen += i; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (fixed_size_len) { | 
					
						
							|  |  |  |         *fixed_size_len = i; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     return totlen; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 16:33:12 +11:00
										 |  |  | bool mqtt_header_complete(uint8_t *buffer, size_t buffer_length) | 
					
						
							| 
									
										
										
										
											2019-04-01 21:22:21 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     uint16_t i; | 
					
						
							|  |  |  |     uint16_t topiclen; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     for (i = 1; i < MQTT_MAX_FIXED_HEADER_SIZE; ++i) { | 
					
						
							|  |  |  |         if (i >= buffer_length) { | 
					
						
							| 
									
										
										
										
											2019-04-01 21:22:21 +02:00
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         if ((buffer[i] & 0x80) == 0) { | 
					
						
							| 
									
										
										
										
											2019-04-01 21:22:21 +02:00
										 |  |  |             ++i; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // i is now the length of the fixed header
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (i + 2 >= buffer_length) { | 
					
						
							| 
									
										
										
										
											2019-04-01 21:22:21 +02:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-01 21:22:21 +02:00
										 |  |  |     topiclen = buffer[i++] << 8; | 
					
						
							|  |  |  |     topiclen |= buffer[i++]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     i += topiclen; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (mqtt_get_qos(buffer) > 0) { | 
					
						
							| 
									
										
										
										
											2019-04-01 21:22:21 +02:00
										 |  |  |         i += 2; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // i is now the length of the fixed + variable header
 | 
					
						
							|  |  |  |     return buffer_length >= i; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 16:33:12 +11:00
										 |  |  | char *mqtt_get_publish_topic(uint8_t *buffer, size_t *length) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     int topiclen; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     for (i = 1; i < *length; ++i) { | 
					
						
							|  |  |  |         if ((buffer[i] & 0x80) == 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |             ++i; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (i + 2 >= *length) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     topiclen = buffer[i++] << 8; | 
					
						
							|  |  |  |     topiclen |= buffer[i++]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (i + topiclen > *length) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     *length = topiclen; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     return (char *)(buffer + i); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 16:33:12 +11:00
										 |  |  | char *mqtt_get_publish_data(uint8_t *buffer, size_t *length) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     int totlen = 0; | 
					
						
							|  |  |  |     int topiclen; | 
					
						
							|  |  |  |     int blength = *length; | 
					
						
							|  |  |  |     *length = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     for (i = 1; i < blength; ++i) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         totlen += (buffer[i] & 0x7f) << (7 * (i - 1)); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         if ((buffer[i] & 0x80) == 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |             ++i; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     totlen += i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (i + 2 >= blength) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     topiclen = buffer[i++] << 8; | 
					
						
							|  |  |  |     topiclen |= buffer[i++]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (i + topiclen >= blength) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     i += topiclen; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (mqtt_get_qos(buffer) > 0) { | 
					
						
							|  |  |  |         if (i + 2 >= blength) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |             return NULL; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         i += 2; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (totlen < i) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (totlen <= blength) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         *length = totlen - i; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         *length = blength - i; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     return (char *)(buffer + i); | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 14:51:21 +02:00
										 |  |  | char *mqtt_get_suback_data(uint8_t *buffer, size_t *length) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // SUBACK payload length = total length - (fixed header (2 bytes) + variable header (2 bytes))
 | 
					
						
							|  |  |  |     // This requires the remaining length to be encoded in 1 byte.
 | 
					
						
							|  |  |  |     if (*length > 4) { | 
					
						
							|  |  |  |         *length -= 4; | 
					
						
							|  |  |  |         return (char *)(buffer + 4); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     *length = 0; | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 16:33:12 +11:00
										 |  |  | uint16_t mqtt_get_id(uint8_t *buffer, size_t length) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (length < 1) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     switch (mqtt_get_type(buffer)) { | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_PUBLISH: { | 
					
						
							|  |  |  |         int i; | 
					
						
							|  |  |  |         int topiclen; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (i = 1; i < length; ++i) { | 
					
						
							|  |  |  |             if ((buffer[i] & 0x80) == 0) { | 
					
						
							|  |  |  |                 ++i; | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (i + 2 >= length) { | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         topiclen = buffer[i++] << 8; | 
					
						
							|  |  |  |         topiclen |= buffer[i++]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (i + topiclen > length) { | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         i += topiclen; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (mqtt_get_qos(buffer) > 0) { | 
					
						
							|  |  |  |             if (i + 2 > length) { | 
					
						
							|  |  |  |                 return 0; | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |             //i += 2;
 | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         return (buffer[i] << 8) | buffer[i + 1]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_PUBACK: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_PUBREC: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_PUBREL: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_PUBCOMP: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_SUBACK: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_UNSUBACK: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_SUBSCRIBE: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_UNSUBSCRIBE: { | 
					
						
							|  |  |  |         // This requires the remaining length to be encoded in 1 byte,
 | 
					
						
							|  |  |  |         // which it should be.
 | 
					
						
							|  |  |  |         if (length >= 4 && (buffer[1] & 0x80) == 0) { | 
					
						
							|  |  |  |             return (buffer[2] << 8) | buffer[3]; | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | mqtt_message_t *mqtt_msg_connect(mqtt_connection_t *connection, mqtt_connect_info_t *info) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     set_message_header_size(connection); | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-09 11:50:40 +08:00
										 |  |  |     int header_len; | 
					
						
							|  |  |  |     if (info->protocol_ver == MQTT_PROTOCOL_V_3_1) { | 
					
						
							|  |  |  |         header_len = MQTT_3_1_VARIABLE_HEADER_SIZE; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         header_len = MQTT_3_1_1_VARIABLE_HEADER_SIZE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     if (connection->outbound_message.length + header_len > connection->buffer_length) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     char *variable_header = (char *)(connection->buffer + connection->outbound_message.length); | 
					
						
							|  |  |  |     connection->outbound_message.length += header_len; | 
					
						
							| 
									
										
										
										
											2020-01-09 11:50:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     int header_idx = 0; | 
					
						
							|  |  |  |     variable_header[header_idx++] = 0;                              // Variable header length MSB
 | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-09 11:50:40 +08:00
										 |  |  |     if (info->protocol_ver == MQTT_PROTOCOL_V_3_1) { | 
					
						
							|  |  |  |         variable_header[header_idx++] = 6;                          // Variable header length LSB
 | 
					
						
							|  |  |  |         memcpy(&variable_header[header_idx], "MQIsdp", 6);          // Protocol name
 | 
					
						
							|  |  |  |         header_idx = header_idx + 6; | 
					
						
							|  |  |  |         variable_header[header_idx++] = 3;                          // Protocol version
 | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         /* Defaults to protocol version 3.1.1 values */ | 
					
						
							|  |  |  |         variable_header[header_idx++] = 4;                          // Variable header length LSB
 | 
					
						
							|  |  |  |         memcpy(&variable_header[header_idx], "MQTT", 4);            // Protocol name
 | 
					
						
							|  |  |  |         header_idx = header_idx + 4; | 
					
						
							|  |  |  |         variable_header[header_idx++] = 4;                          // Protocol version
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-09 11:50:40 +08:00
										 |  |  |     int flags_offset = header_idx; | 
					
						
							|  |  |  |     variable_header[header_idx++] = 0;                              // Flags
 | 
					
						
							|  |  |  |     variable_header[header_idx++] = info->keepalive >> 8;           // Keep-alive MSB
 | 
					
						
							|  |  |  |     variable_header[header_idx] = info->keepalive & 0xff;         // Keep-alive LSB
 | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (info->clean_session) { | 
					
						
							| 
									
										
										
										
											2020-01-09 11:50:40 +08:00
										 |  |  |         variable_header[flags_offset] |= MQTT_CONNECT_FLAG_CLEAN_SESSION; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (info->client_id != NULL && info->client_id[0] != '\0') { | 
					
						
							|  |  |  |         if (append_string(connection, info->client_id, strlen(info->client_id)) < 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |             return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2021-10-14 11:00:45 -03:00
										 |  |  |         if (append_string(connection, "", 0) < 0) { | 
					
						
							|  |  |  |             return fail_message(connection); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (info->will_topic != NULL && info->will_topic[0] != '\0') { | 
					
						
							|  |  |  |         if (append_string(connection, info->will_topic, strlen(info->will_topic)) < 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |             return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         if (append_string(connection, info->will_message, info->will_length) < 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |             return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-09 11:50:40 +08:00
										 |  |  |         variable_header[flags_offset] |= MQTT_CONNECT_FLAG_WILL; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         if (info->will_retain) { | 
					
						
							| 
									
										
										
										
											2020-01-09 11:50:40 +08:00
										 |  |  |             variable_header[flags_offset] |= MQTT_CONNECT_FLAG_WILL_RETAIN; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-09 11:50:40 +08:00
										 |  |  |         variable_header[flags_offset] |= (info->will_qos & 3) << 3; | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (info->username != NULL && info->username[0] != '\0') { | 
					
						
							|  |  |  |         if (append_string(connection, info->username, strlen(info->username)) < 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |             return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-09 11:50:40 +08:00
										 |  |  |         variable_header[flags_offset] |= MQTT_CONNECT_FLAG_USERNAME; | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (info->password != NULL && info->password[0] != '\0') { | 
					
						
							| 
									
										
										
										
											2020-12-23 21:14:25 +08:00
										 |  |  |         if (info->username == NULL || info->username[0] == '\0') { | 
					
						
							|  |  |  |             /* In case if password is set without username, we need to set a zero length username.
 | 
					
						
							|  |  |  |              * (otherwise we violate: MQTT-3.1.2-22: If the User Name Flag is set to 0 then the Password Flag MUST be set to 0.) | 
					
						
							|  |  |  |              */ | 
					
						
							|  |  |  |             if (append_string(connection, "", 0) < 0) { | 
					
						
							|  |  |  |                 return fail_message(connection); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             variable_header[flags_offset] |= MQTT_CONNECT_FLAG_USERNAME; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         if (append_string(connection, info->password, strlen(info->password)) < 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |             return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-09 11:50:40 +08:00
										 |  |  |         variable_header[flags_offset] |= MQTT_CONNECT_FLAG_PASSWORD; | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return fini_message(connection, MQTT_MSG_TYPE_CONNECT, 0, 0, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | mqtt_message_t *mqtt_msg_publish(mqtt_connection_t *connection, const char *topic, const char *data, int data_length, int qos, int retain, uint16_t *message_id) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     set_message_header_size(connection); | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (topic == NULL || topic[0] == '\0') { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (append_string(connection, topic, strlen(topic)) < 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 12:05:29 +02:00
										 |  |  |     if (data == NULL && data_length > 0) { | 
					
						
							| 
									
										
										
										
											2021-10-14 11:00:45 -03:00
										 |  |  |         return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-03 12:05:29 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (qos > 0) { | 
					
						
							|  |  |  |         if ((*message_id = append_message_id(connection, 0)) == 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |             return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         *message_id = 0; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-15 10:50:25 +01:00
										 |  |  |     if (data != NULL) { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |         if (connection->outbound_message.length + data_length > connection->buffer_length) { | 
					
						
							| 
									
										
										
										
											2022-12-15 10:50:25 +01:00
										 |  |  |             // Not enough size in buffer -> fragment this message
 | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |             connection->outbound_message.fragmented_msg_data_offset = connection->outbound_message.length; | 
					
						
							|  |  |  |             memcpy(connection->buffer + connection->outbound_message.length, data, connection->buffer_length - connection->outbound_message.length); | 
					
						
							|  |  |  |             connection->outbound_message.length = connection->buffer_length; | 
					
						
							|  |  |  |             connection->outbound_message.fragmented_msg_total_length = data_length + connection->outbound_message.fragmented_msg_data_offset; | 
					
						
							| 
									
										
										
										
											2022-12-15 10:50:25 +01:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |             memcpy(connection->buffer + connection->outbound_message.length, data, data_length); | 
					
						
							|  |  |  |             connection->outbound_message.length += data_length; | 
					
						
							|  |  |  |             connection->outbound_message.fragmented_msg_total_length = 0; | 
					
						
							| 
									
										
										
										
											2019-05-03 12:06:46 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-10-25 16:38:25 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     return fini_message(connection, MQTT_MSG_TYPE_PUBLISH, 0, qos, retain); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | mqtt_message_t *mqtt_msg_puback(mqtt_connection_t *connection, uint16_t message_id) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     set_message_header_size(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (append_message_id(connection, message_id) == 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     return fini_message(connection, MQTT_MSG_TYPE_PUBACK, 0, 0, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | mqtt_message_t *mqtt_msg_pubrec(mqtt_connection_t *connection, uint16_t message_id) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     set_message_header_size(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (append_message_id(connection, message_id) == 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     return fini_message(connection, MQTT_MSG_TYPE_PUBREC, 0, 0, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | mqtt_message_t *mqtt_msg_pubrel(mqtt_connection_t *connection, uint16_t message_id) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     set_message_header_size(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (append_message_id(connection, message_id) == 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     return fini_message(connection, MQTT_MSG_TYPE_PUBREL, 0, 1, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | mqtt_message_t *mqtt_msg_pubcomp(mqtt_connection_t *connection, uint16_t message_id) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     set_message_header_size(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (append_message_id(connection, message_id) == 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     return fini_message(connection, MQTT_MSG_TYPE_PUBCOMP, 0, 0, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 15:27:40 +01:00
										 |  |  | mqtt_message_t *mqtt_msg_subscribe(mqtt_connection_t *connection, const esp_mqtt_topic_t topic_list[], int size, uint16_t *message_id) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     set_message_header_size(connection); | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if ((*message_id = append_message_id(connection, 0)) == 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 15:27:40 +01:00
										 |  |  |     for (int topic_number = 0; topic_number < size; ++topic_number) { | 
					
						
							|  |  |  |         if (topic_list[topic_number].filter[0] == '\0') { | 
					
						
							|  |  |  |             return fail_message(connection); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-12 15:27:40 +01:00
										 |  |  |         if (append_string(connection, topic_list[topic_number].filter, strlen(topic_list[topic_number].filter)) < 0) { | 
					
						
							|  |  |  |             return fail_message(connection); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |         if (connection->outbound_message.length + 1 > connection->buffer_length) { | 
					
						
							| 
									
										
										
										
											2022-12-12 15:27:40 +01:00
										 |  |  |             return fail_message(connection); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |         connection->buffer[connection->outbound_message.length] = topic_list[topic_number].qos; | 
					
						
							|  |  |  |         connection->outbound_message.length ++; | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return fini_message(connection, MQTT_MSG_TYPE_SUBSCRIBE, 0, 1, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | mqtt_message_t *mqtt_msg_unsubscribe(mqtt_connection_t *connection, const char *topic, uint16_t *message_id) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     set_message_header_size(connection); | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (topic == NULL || topic[0] == '\0') { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if ((*message_id = append_message_id(connection, 0)) == 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     if (append_string(connection, topic, strlen(topic)) < 0) { | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |         return fail_message(connection); | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return fini_message(connection, MQTT_MSG_TYPE_UNSUBSCRIBE, 0, 1, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | mqtt_message_t *mqtt_msg_pingreq(mqtt_connection_t *connection) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     set_message_header_size(connection); | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     return fini_message(connection, MQTT_MSG_TYPE_PINGREQ, 0, 0, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | mqtt_message_t *mqtt_msg_pingresp(mqtt_connection_t *connection) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     set_message_header_size(connection); | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     return fini_message(connection, MQTT_MSG_TYPE_PINGRESP, 0, 0, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  | mqtt_message_t *mqtt_msg_disconnect(mqtt_connection_t *connection) | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     set_message_header_size(connection); | 
					
						
							| 
									
										
										
										
											2016-09-11 10:10:24 +07:00
										 |  |  |     return fini_message(connection, MQTT_MSG_TYPE_DISCONNECT, 0, 0, 0); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-26 16:38:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * check flags: [MQTT-2.2.2-1], [MQTT-2.2.2-2] | 
					
						
							|  |  |  |  * returns 0 if flags are invalid, otherwise returns 1 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-10-09 16:33:12 +11:00
										 |  |  | int mqtt_has_valid_msg_hdr(uint8_t *buffer, size_t length) | 
					
						
							| 
									
										
										
										
											2019-04-26 16:38:09 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     int qos, dup; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (length < 1) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-06 13:45:33 +02:00
										 |  |  |     switch (mqtt_get_type(buffer)) { | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_CONNECT: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_CONNACK: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_PUBACK: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_PUBREC: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_PUBCOMP: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_SUBACK: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_UNSUBACK: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_PINGREQ: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_PINGRESP: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_DISCONNECT: | 
					
						
							|  |  |  |         return (buffer[0] & 0x0f) == 0;  /* all flag bits are 0 */ | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_PUBREL: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_SUBSCRIBE: | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_UNSUBSCRIBE: | 
					
						
							|  |  |  |         return (buffer[0] & 0x0f) == 0x02;  /* only bit 1 is set */ | 
					
						
							|  |  |  |     case MQTT_MSG_TYPE_PUBLISH: | 
					
						
							|  |  |  |         qos = mqtt_get_qos(buffer); | 
					
						
							|  |  |  |         dup = mqtt_get_dup(buffer); | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |          * there is no qos=3  [MQTT-3.3.1-4] | 
					
						
							|  |  |  |          * dup flag must be set to 0 for all qos=0 messages [MQTT-3.3.1-2] | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         return (qos < 3) && ((qos > 0) || (dup == 0)); | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2019-04-26 16:38:09 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-08 14:59:22 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-30 15:16:14 +02:00
										 |  |  | esp_err_t mqtt_msg_buffer_init(mqtt_connection_t *connection, int buffer_size) | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  | { | 
					
						
							|  |  |  |     memset(connection, 0, sizeof(mqtt_connection_t)); | 
					
						
							| 
									
										
										
										
											2023-06-21 08:23:24 +02:00
										 |  |  |     connection->buffer = (uint8_t *)calloc(buffer_size, sizeof(uint8_t)); | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  |     if (!connection->buffer) { | 
					
						
							|  |  |  |         return ESP_ERR_NO_MEM; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     connection->buffer_length = buffer_size; | 
					
						
							|  |  |  |     return ESP_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-30 15:16:14 +02:00
										 |  |  | void mqtt_msg_buffer_destroy(mqtt_connection_t *connection) | 
					
						
							| 
									
										
										
										
											2022-08-17 13:41:35 -03:00
										 |  |  | { | 
					
						
							|  |  |  |     if (connection) { | 
					
						
							|  |  |  |         free(connection->buffer); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |