mirror of
https://github.com/espressif/esp-mqtt.git
synced 2025-07-29 18:28:24 +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
@ -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