Skip to content

Commit

Permalink
refactor: Group access to output buffer in mqtt_connection_t
Browse files Browse the repository at this point in the history
  - Moves mqtt_connect_info to mqtt_connection_t.
  - Removes outbound_message in favor of accessing it trough connection.
  • Loading branch information
euripedesrocha committed Jun 13, 2023
1 parent ed62809 commit 122875b
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 342 deletions.
6 changes: 1 addition & 5 deletions lib/include/mqtt_client_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,10 @@ extern "C" {

typedef struct mqtt_state {
uint8_t *in_buffer;
uint8_t *out_buffer;
int in_buffer_length;
int out_buffer_length;
size_t message_length;
size_t in_buffer_read_len;
mqtt_message_t *outbound_message;
mqtt_connection_t mqtt_connection;
mqtt_connection_t connection;
uint16_t pending_msg_id;
int pending_msg_type;
int pending_publish_qos;
Expand Down Expand Up @@ -106,7 +103,6 @@ struct esp_mqtt_client {
esp_transport_handle_t transport;
mqtt_config_storage_t *config;
mqtt_state_t mqtt_state;
mqtt_connect_info_t connect_info;
mqtt_client_state_t state;
uint64_t refresh_connection_tick;
int64_t keepalive_tick;
Expand Down
27 changes: 13 additions & 14 deletions lib/include/mqtt_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@ typedef struct mqtt_message {
size_t fragmented_msg_data_offset; /*!< data offset of fragmented messages (zero for all other messages) */
} mqtt_message_t;

typedef struct mqtt_connection {
mqtt_message_t message;
#if MQTT_MSG_ID_INCREMENTAL
uint16_t last_message_id; /*!< last used id if incremental message id configured */
#endif
uint8_t *buffer;
size_t buffer_length;

} mqtt_connection_t;

typedef struct mqtt_connect_info {
char *client_id;
char *username;
Expand All @@ -90,9 +80,18 @@ typedef struct mqtt_connect_info {
int will_retain;
int clean_session;
esp_mqtt_protocol_ver_t protocol_ver;

} mqtt_connect_info_t;

typedef struct mqtt_connection {
mqtt_message_t outbound_message;
#if MQTT_MSG_ID_INCREMENTAL
uint16_t last_message_id; /*!< last used id if incremental message id configured */
#endif
uint8_t *buffer;
size_t buffer_length;
mqtt_connect_info_t information;

} mqtt_connection_t;

static inline int mqtt_get_type(const uint8_t *buffer)
{
Expand Down Expand Up @@ -123,7 +122,6 @@ static inline int mqtt_get_retain(const uint8_t *buffer)
return (buffer[0] & 0x01);
}

void mqtt_msg_init(mqtt_connection_t *connection, uint8_t *buffer, size_t buffer_length);
bool mqtt_header_complete(uint8_t *buffer, size_t buffer_length);
size_t mqtt_get_total_length(const uint8_t *buffer, size_t length, int *fixed_size_len);
char *mqtt_get_publish_topic(uint8_t *buffer, size_t *length);
Expand All @@ -132,6 +130,9 @@ char *mqtt_get_suback_data(uint8_t *buffer, size_t *length);
uint16_t mqtt_get_id(uint8_t *buffer, size_t length);
int mqtt_has_valid_msg_hdr(uint8_t *buffer, size_t length);

esp_err_t mqtt_connection_init(mqtt_connection_t *connection, int buffer_size);
void mqtt_connection_destroy(mqtt_connection_t *connection);

mqtt_message_t *mqtt_msg_connect(mqtt_connection_t *connection, mqtt_connect_info_t *info);
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);
mqtt_message_t *mqtt_msg_puback(mqtt_connection_t *connection, uint16_t message_id);
Expand All @@ -143,8 +144,6 @@ mqtt_message_t *mqtt_msg_unsubscribe(mqtt_connection_t *connection, const char *
mqtt_message_t *mqtt_msg_pingreq(mqtt_connection_t *connection);
mqtt_message_t *mqtt_msg_pingresp(mqtt_connection_t *connection);
mqtt_message_t *mqtt_msg_disconnect(mqtt_connection_t *connection);


#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 122875b

Please sign in to comment.