mirror of
https://github.com/espressif/esp-mqtt.git
synced 2025-06-25 09:21:40 +02:00
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.
This commit is contained in:
committed by
Rocha Euripedes
parent
ffd7d4df6c
commit
a5c1b441dc
@ -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();
|
||||
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include <string.h>
|
||||
#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
|
||||
|
@ -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 {
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "mqtt_client.h"
|
||||
#include "esp_transport.h"
|
||||
#include "mqtt_client_priv.h"
|
||||
#include "esp_log.h"
|
||||
#include <stdint.h>
|
||||
@ -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);
|
||||
|
Reference in New Issue
Block a user