mirror of
https://github.com/espressif/esp-mqtt.git
synced 2025-07-31 03:08:03 +02:00
fix: Move buffer initialization to set config
When calling set config message buffers were not affected because the creation was handled on init. Closes https://github.com/espressif/esp-mqtt/issues/267
This commit is contained in:
committed by
Rocha Euripedes
parent
abd8b6cadc
commit
ea0df31e00
@ -597,6 +597,9 @@ esp_err_t esp_mqtt_client_destroy(esp_mqtt_client_handle_t client);
|
|||||||
* @brief Set configuration structure, typically used when updating the config
|
* @brief Set configuration structure, typically used when updating the config
|
||||||
* (i.e. on "before_connect" event
|
* (i.e. on "before_connect" event
|
||||||
*
|
*
|
||||||
|
* Notes:
|
||||||
|
* - When calling this function make sure to have all the intendend configurations
|
||||||
|
* set, otherwise default values are set.
|
||||||
* @param client *MQTT* client handle
|
* @param client *MQTT* client handle
|
||||||
*
|
*
|
||||||
* @param config *MQTT* configuration structure
|
* @param config *MQTT* configuration structure
|
||||||
|
@ -100,6 +100,7 @@
|
|||||||
#define MQTT_ENABLE_SSL CONFIG_MQTT_TRANSPORT_SSL
|
#define MQTT_ENABLE_SSL CONFIG_MQTT_TRANSPORT_SSL
|
||||||
#define MQTT_ENABLE_WS CONFIG_MQTT_TRANSPORT_WEBSOCKET
|
#define MQTT_ENABLE_WS CONFIG_MQTT_TRANSPORT_WEBSOCKET
|
||||||
#define MQTT_ENABLE_WSS CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE
|
#define MQTT_ENABLE_WSS CONFIG_MQTT_TRANSPORT_WEBSOCKET_SECURE
|
||||||
|
#define MQTT_DEFAULT_RETRANSMIT_TIMEOUT_MS 1000
|
||||||
|
|
||||||
#ifdef CONFIG_MQTT_EVENT_QUEUE_SIZE
|
#ifdef CONFIG_MQTT_EVENT_QUEUE_SIZE
|
||||||
#define MQTT_EVENT_QUEUE_SIZE CONFIG_MQTT_EVENT_QUEUE_SIZE
|
#define MQTT_EVENT_QUEUE_SIZE CONFIG_MQTT_EVENT_QUEUE_SIZE
|
||||||
|
@ -618,7 +618,7 @@ int mqtt_has_valid_msg_hdr(uint8_t *buffer, size_t length)
|
|||||||
|
|
||||||
esp_err_t mqtt_msg_buffer_init(mqtt_connection_t *connection, int buffer_size)
|
esp_err_t mqtt_msg_buffer_init(mqtt_connection_t *connection, int buffer_size)
|
||||||
{
|
{
|
||||||
memset(connection, 0, sizeof(mqtt_connection_t));
|
memset(&connection->outbound_message, 0, sizeof(mqtt_message_t));
|
||||||
connection->buffer = (uint8_t *)calloc(buffer_size, sizeof(uint8_t));
|
connection->buffer = (uint8_t *)calloc(buffer_size, sizeof(uint8_t));
|
||||||
if (!connection->buffer) {
|
if (!connection->buffer) {
|
||||||
return ESP_ERR_NO_MEM;
|
return ESP_ERR_NO_MEM;
|
||||||
|
@ -383,9 +383,26 @@ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_cl
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mqtt_msg_buffer_destroy(&client->mqtt_state.connection);
|
||||||
|
int buffer_size = config->buffer.size;
|
||||||
|
if (buffer_size <= 0) {
|
||||||
|
buffer_size = MQTT_BUFFER_SIZE_BYTE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// use separate value for output buffer size if configured
|
||||||
|
int out_buffer_size = config->buffer.out_size > 0 ? config->buffer.out_size : buffer_size;
|
||||||
|
if (mqtt_msg_buffer_init(&client->mqtt_state.connection, out_buffer_size) != ESP_OK) {
|
||||||
|
goto _mqtt_set_config_failed;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(client->mqtt_state.in_buffer);
|
||||||
|
client->mqtt_state.in_buffer = (uint8_t *)malloc(buffer_size);
|
||||||
|
ESP_MEM_CHECK(TAG, client->mqtt_state.in_buffer, goto _mqtt_set_config_failed);
|
||||||
|
client->mqtt_state.in_buffer_length = buffer_size;
|
||||||
|
|
||||||
client->config->message_retransmit_timeout = config->session.message_retransmit_timeout;
|
client->config->message_retransmit_timeout = config->session.message_retransmit_timeout;
|
||||||
if (config->session.message_retransmit_timeout <= 0) {
|
if (config->session.message_retransmit_timeout <= 0) {
|
||||||
client->config->message_retransmit_timeout = 1000;
|
client->config->message_retransmit_timeout = MQTT_DEFAULT_RETRANSMIT_TIMEOUT_MS;
|
||||||
}
|
}
|
||||||
|
|
||||||
client->config->task_prio = config->task.priority;
|
client->config->task_prio = config->task.priority;
|
||||||
@ -599,6 +616,8 @@ void esp_mqtt_destroy_config(esp_mqtt_client_handle_t client)
|
|||||||
if (client->config == NULL) {
|
if (client->config == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
free(client->mqtt_state.in_buffer);
|
||||||
|
mqtt_msg_buffer_destroy(&client->mqtt_state.connection);
|
||||||
free(client->config->host);
|
free(client->config->host);
|
||||||
free(client->config->uri);
|
free(client->config->uri);
|
||||||
free(client->config->path);
|
free(client->config->path);
|
||||||
@ -807,6 +826,11 @@ static bool create_client_data(esp_mqtt_client_handle_t client)
|
|||||||
client->api_lock = xSemaphoreCreateRecursiveMutex();
|
client->api_lock = xSemaphoreCreateRecursiveMutex();
|
||||||
ESP_MEM_CHECK(TAG, client->api_lock, return false);
|
ESP_MEM_CHECK(TAG, client->api_lock, return false);
|
||||||
|
|
||||||
|
client->outbox = outbox_init();
|
||||||
|
ESP_MEM_CHECK(TAG, client->outbox, return false);
|
||||||
|
client->status_bits = xEventGroupCreate();
|
||||||
|
ESP_MEM_CHECK(TAG, client->status_bits, return false);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -824,24 +848,6 @@ esp_mqtt_client_handle_t esp_mqtt_client_init(const esp_mqtt_client_config_t *co
|
|||||||
if (!create_client_data(client)) {
|
if (!create_client_data(client)) {
|
||||||
goto _mqtt_init_failed;
|
goto _mqtt_init_failed;
|
||||||
}
|
}
|
||||||
int buffer_size = config->buffer.size;
|
|
||||||
if (buffer_size <= 0) {
|
|
||||||
buffer_size = MQTT_BUFFER_SIZE_BYTE;
|
|
||||||
}
|
|
||||||
|
|
||||||
// use separate value for output buffer size if configured
|
|
||||||
int out_buffer_size = config->buffer.out_size > 0 ? config->buffer.out_size : buffer_size;
|
|
||||||
if (mqtt_msg_buffer_init(&client->mqtt_state.connection, out_buffer_size) != ESP_OK) {
|
|
||||||
goto _mqtt_init_failed;
|
|
||||||
}
|
|
||||||
|
|
||||||
client->mqtt_state.in_buffer = (uint8_t *)malloc(buffer_size);
|
|
||||||
ESP_MEM_CHECK(TAG, client->mqtt_state.in_buffer, goto _mqtt_init_failed);
|
|
||||||
client->mqtt_state.in_buffer_length = buffer_size;
|
|
||||||
client->outbox = outbox_init();
|
|
||||||
ESP_MEM_CHECK(TAG, client->outbox, goto _mqtt_init_failed);
|
|
||||||
client->status_bits = xEventGroupCreate();
|
|
||||||
ESP_MEM_CHECK(TAG, client->status_bits, goto _mqtt_init_failed);
|
|
||||||
|
|
||||||
if (esp_mqtt_set_config(client, config) != ESP_OK) {
|
if (esp_mqtt_set_config(client, config) != ESP_OK) {
|
||||||
goto _mqtt_init_failed;
|
goto _mqtt_init_failed;
|
||||||
@ -890,8 +896,6 @@ esp_err_t esp_mqtt_client_destroy(esp_mqtt_client_handle_t client)
|
|||||||
if (client->status_bits) {
|
if (client->status_bits) {
|
||||||
vEventGroupDelete(client->status_bits);
|
vEventGroupDelete(client->status_bits);
|
||||||
}
|
}
|
||||||
free(client->mqtt_state.in_buffer);
|
|
||||||
mqtt_msg_buffer_destroy(&client->mqtt_state.connection);
|
|
||||||
if (client->api_lock) {
|
if (client->api_lock) {
|
||||||
vSemaphoreDelete(client->api_lock);
|
vSemaphoreDelete(client->api_lock);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user