Skip to content

Commit 7894dd0

Browse files
Merge branch 'fix/buffer_creation_in_set_config' into 'master'
fix: Move buffer initialization to set config See merge request espressif/esp-mqtt!194
2 parents abd8b6c + ea0df31 commit 7894dd0

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

include/mqtt_client.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,9 @@ esp_err_t esp_mqtt_client_destroy(esp_mqtt_client_handle_t client);
597597
* @brief Set configuration structure, typically used when updating the config
598598
* (i.e. on "before_connect" event
599599
*
600+
* Notes:
601+
* - When calling this function make sure to have all the intendend configurations
602+
* set, otherwise default values are set.
600603
* @param client *MQTT* client handle
601604
*
602605
* @param config *MQTT* configuration structure

lib/include/mqtt_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
#define MQTT_ENABLE_SSL CONFIG_MQTT_TRANSPORT_SSL
101101
#define MQTT_ENABLE_WS CONFIG_MQTT_TRANSPORT_WEBSOCKET
102102
#define MQTT_ENABLE_WSS CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE
103+
#define MQTT_DEFAULT_RETRANSMIT_TIMEOUT_MS 1000
103104

104105
#ifdef CONFIG_MQTT_EVENT_QUEUE_SIZE
105106
#define MQTT_EVENT_QUEUE_SIZE CONFIG_MQTT_EVENT_QUEUE_SIZE

lib/mqtt_msg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ int mqtt_has_valid_msg_hdr(uint8_t *buffer, size_t length)
618618

619619
esp_err_t mqtt_msg_buffer_init(mqtt_connection_t *connection, int buffer_size)
620620
{
621-
memset(connection, 0, sizeof(mqtt_connection_t));
621+
memset(&connection->outbound_message, 0, sizeof(mqtt_message_t));
622622
connection->buffer = (uint8_t *)calloc(buffer_size, sizeof(uint8_t));
623623
if (!connection->buffer) {
624624
return ESP_ERR_NO_MEM;

mqtt_client.c

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,26 @@ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_cl
383383
});
384384
}
385385

386+
mqtt_msg_buffer_destroy(&client->mqtt_state.connection);
387+
int buffer_size = config->buffer.size;
388+
if (buffer_size <= 0) {
389+
buffer_size = MQTT_BUFFER_SIZE_BYTE;
390+
}
391+
392+
// use separate value for output buffer size if configured
393+
int out_buffer_size = config->buffer.out_size > 0 ? config->buffer.out_size : buffer_size;
394+
if (mqtt_msg_buffer_init(&client->mqtt_state.connection, out_buffer_size) != ESP_OK) {
395+
goto _mqtt_set_config_failed;
396+
}
397+
398+
free(client->mqtt_state.in_buffer);
399+
client->mqtt_state.in_buffer = (uint8_t *)malloc(buffer_size);
400+
ESP_MEM_CHECK(TAG, client->mqtt_state.in_buffer, goto _mqtt_set_config_failed);
401+
client->mqtt_state.in_buffer_length = buffer_size;
402+
386403
client->config->message_retransmit_timeout = config->session.message_retransmit_timeout;
387404
if (config->session.message_retransmit_timeout <= 0) {
388-
client->config->message_retransmit_timeout = 1000;
405+
client->config->message_retransmit_timeout = MQTT_DEFAULT_RETRANSMIT_TIMEOUT_MS;
389406
}
390407

391408
client->config->task_prio = config->task.priority;
@@ -599,6 +616,8 @@ void esp_mqtt_destroy_config(esp_mqtt_client_handle_t client)
599616
if (client->config == NULL) {
600617
return;
601618
}
619+
free(client->mqtt_state.in_buffer);
620+
mqtt_msg_buffer_destroy(&client->mqtt_state.connection);
602621
free(client->config->host);
603622
free(client->config->uri);
604623
free(client->config->path);
@@ -807,6 +826,11 @@ static bool create_client_data(esp_mqtt_client_handle_t client)
807826
client->api_lock = xSemaphoreCreateRecursiveMutex();
808827
ESP_MEM_CHECK(TAG, client->api_lock, return false);
809828

829+
client->outbox = outbox_init();
830+
ESP_MEM_CHECK(TAG, client->outbox, return false);
831+
client->status_bits = xEventGroupCreate();
832+
ESP_MEM_CHECK(TAG, client->status_bits, return false);
833+
810834
return true;
811835
}
812836

@@ -824,24 +848,6 @@ esp_mqtt_client_handle_t esp_mqtt_client_init(const esp_mqtt_client_config_t *co
824848
if (!create_client_data(client)) {
825849
goto _mqtt_init_failed;
826850
}
827-
int buffer_size = config->buffer.size;
828-
if (buffer_size <= 0) {
829-
buffer_size = MQTT_BUFFER_SIZE_BYTE;
830-
}
831-
832-
// use separate value for output buffer size if configured
833-
int out_buffer_size = config->buffer.out_size > 0 ? config->buffer.out_size : buffer_size;
834-
if (mqtt_msg_buffer_init(&client->mqtt_state.connection, out_buffer_size) != ESP_OK) {
835-
goto _mqtt_init_failed;
836-
}
837-
838-
client->mqtt_state.in_buffer = (uint8_t *)malloc(buffer_size);
839-
ESP_MEM_CHECK(TAG, client->mqtt_state.in_buffer, goto _mqtt_init_failed);
840-
client->mqtt_state.in_buffer_length = buffer_size;
841-
client->outbox = outbox_init();
842-
ESP_MEM_CHECK(TAG, client->outbox, goto _mqtt_init_failed);
843-
client->status_bits = xEventGroupCreate();
844-
ESP_MEM_CHECK(TAG, client->status_bits, goto _mqtt_init_failed);
845851

846852
if (esp_mqtt_set_config(client, config) != ESP_OK) {
847853
goto _mqtt_init_failed;
@@ -890,8 +896,6 @@ esp_err_t esp_mqtt_client_destroy(esp_mqtt_client_handle_t client)
890896
if (client->status_bits) {
891897
vEventGroupDelete(client->status_bits);
892898
}
893-
free(client->mqtt_state.in_buffer);
894-
mqtt_msg_buffer_destroy(&client->mqtt_state.connection);
895899
if (client->api_lock) {
896900
vSemaphoreDelete(client->api_lock);
897901
}

0 commit comments

Comments
 (0)