From a5c1b441dcb9be0bd049adcdf6856595a4b54752 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Wed, 3 May 2023 09:49:17 +0200 Subject: [PATCH] feat: Add custom transport configuration Today there is no way to add a new transport without applying modifications to the transport list. This impose limitations on the client usage. Adding the custom configuration we enable user defined transports. --- host_test/main/test_mqtt_client.cpp | 2 ++ include/mqtt_client.h | 2 ++ lib/include/mqtt_client_priv.h | 1 + mqtt_client.c | 47 +++++++++++++++++------------ 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/host_test/main/test_mqtt_client.cpp b/host_test/main/test_mqtt_client.cpp index 37dd34e..55d0f2b 100644 --- a/host_test/main/test_mqtt_client.cpp +++ b/host_test/main/test_mqtt_client.cpp @@ -3,6 +3,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ +#include "esp_transport.h" #define CATCH_CONFIG_MAIN // This tells the catch header to generate a main #include "catch.hpp" @@ -60,6 +61,7 @@ struct ClientInitializedFixture { esp_read_mac_IgnoreAndReturn(ESP_OK); esp_read_mac_ReturnThruPtr_mac(mac); esp_transport_list_destroy_IgnoreAndReturn(ESP_OK); + esp_transport_destroy_IgnoreAndReturn(ESP_OK); vEventGroupDelete_Ignore(); vQueueDelete_Ignore(); diff --git a/include/mqtt_client.h b/include/mqtt_client.h index a5933af..23e56b6 100644 --- a/include/mqtt_client.h +++ b/include/mqtt_client.h @@ -12,6 +12,7 @@ #include #include "esp_err.h" #include "esp_event.h" +#include "esp_transport.h" #ifdef CONFIG_MQTT_PROTOCOL_5 #include "mqtt5_client.h" #endif @@ -329,6 +330,7 @@ typedef struct esp_mqtt_client_config_t { int refresh_connection_after_ms; /*!< Refresh connection after this value (in milliseconds) */ bool disable_auto_reconnect; /*!< Client will reconnect to server (when errors/disconnect). Set `disable_auto_reconnect=true` to disable */ + esp_transport_handle_t transport; /*!< Custom transport handle to use. Warning: The transport should be valid during the client lifetime and is destroyed when esp_mqtt_client_destroy is called. */ } network; /*!< Network configuration */ /** * Client task configuration diff --git a/lib/include/mqtt_client_priv.h b/lib/include/mqtt_client_priv.h index bf09ceb..a91787d 100644 --- a/lib/include/mqtt_client_priv.h +++ b/lib/include/mqtt_client_priv.h @@ -91,6 +91,7 @@ typedef struct { bool use_secure_element; void *ds_data; int message_retransmit_timeout; + esp_transport_handle_t transport; } mqtt_config_storage_t; typedef enum { diff --git a/mqtt_client.c b/mqtt_client.c index 504edd6..c904f00 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -1,4 +1,5 @@ #include "mqtt_client.h" +#include "esp_transport.h" #include "mqtt_client_priv.h" #include "esp_log.h" #include @@ -475,6 +476,9 @@ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_cl } else { client->config->reconnect_timeout_ms = MQTT_RECON_DEFAULT_MS; } + if (config->network.transport) { + client->config->transport = config->network.transport; + } if (config->broker.verification.alpn_protos) { for (int i = 0; i < client->config->num_alpn_protos; i++) { @@ -592,6 +596,7 @@ void esp_mqtt_destroy_config(esp_mqtt_client_handle_t client) esp_event_loop_delete(client->config->event_loop_handle); } #endif + esp_transport_destroy(client->config->transport); memset(client->config, 0, sizeof(mqtt_config_storage_t)); free(client->config); client->config = NULL; @@ -1513,18 +1518,6 @@ static void esp_mqtt_task(void *pv) outbox_tick_t msg_tick = 0; client->run = true; - //get transport by scheme - client->transport = esp_transport_list_get_transport(client->transport_list, client->config->scheme); - - if (client->transport == NULL) { - ESP_LOGE(TAG, "There are no transports valid, stop mqtt client, config scheme = %s", client->config->scheme); - client->run = false; - } - //default port - if (client->config->port == 0) { - client->config->port = esp_transport_get_default_port(client->transport); - } - client->state = MQTT_STATE_INIT; xEventGroupClearBits(client->status_bits, STOPPED_BIT); while (client->run) { @@ -1538,10 +1531,29 @@ static void esp_mqtt_task(void *pv) client->event.event_id = MQTT_EVENT_BEFORE_CONNECT; esp_mqtt_dispatch_event_with_msgid(client); - if (client->transport == NULL) { - ESP_LOGE(TAG, "There is no transport"); - client->run = false; + + client->transport = client->config->transport; + if (!client->transport) { + + if (esp_mqtt_client_create_transport(client) != ESP_OK) { + ESP_LOGE(TAG, "Failed to create transport list"); + client->run = false; + break; + } + //get transport by scheme + client->transport = esp_transport_list_get_transport(client->transport_list, client->config->scheme); + + if (client->transport == NULL) { + ESP_LOGE(TAG, "There are no transports valid, stop mqtt client, config scheme = %s", client->config->scheme); + client->run = false; + break; + } } + //default port + if (client->config->port == 0) { + client->config->port = esp_transport_get_default_port(client->transport); + } + #if MQTT_ENABLE_SSL esp_mqtt_set_ssl_transport_properties(client->transport_list, client->config); #endif @@ -1667,11 +1679,6 @@ esp_err_t esp_mqtt_client_start(esp_mqtt_client_handle_t client) MQTT_API_UNLOCK(client); return ESP_FAIL; } - if (esp_mqtt_client_create_transport(client) != ESP_OK) { - ESP_LOGE(TAG, "Failed to create transport list"); - MQTT_API_UNLOCK(client); - return ESP_FAIL; - } esp_err_t err = ESP_OK; #if MQTT_CORE_SELECTION_ENABLED ESP_LOGD(TAG, "Core selection enabled on %u", MQTT_TASK_CORE);