Skip to content

Commit

Permalink
Merge branch 'feat/get_transport' into 'master'
Browse files Browse the repository at this point in the history
feat: Allow users to get the transport in use

See merge request espressif/esp-mqtt!216
  • Loading branch information
euripedesrocha committed Jul 10, 2024
2 parents 72d7bc7 + 97dc85a commit 1420c01
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
26 changes: 26 additions & 0 deletions include/mqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ typedef void *esp_event_handler_t;

typedef struct esp_mqtt_client *esp_mqtt_client_handle_t;

#define MQTT_OVER_TCP_SCHEME "mqtt"
#define MQTT_OVER_SSL_SCHEME "mqtts"
#define MQTT_OVER_WS_SCHEME "ws"
#define MQTT_OVER_WSS_SCHEME "wss"

/**
* @brief *MQTT* event types.
*
Expand Down Expand Up @@ -661,6 +666,27 @@ int esp_mqtt_client_get_outbox_size(esp_mqtt_client_handle_t client);
*/
esp_err_t esp_mqtt_dispatch_custom_event(esp_mqtt_client_handle_t client, esp_mqtt_event_t *event);

/**
* @brief Get a transport from the scheme
*
* Allows extra settings to be made on the selected transport,
* for convenience the scheme used by the mqtt client are defined as
* MQTT_OVER_TCP_SCHEME, MQTT_OVER_SSL_SCHEME, MQTT_OVER_WS_SCHEME and MQTT_OVER_WSS_SCHEME
* If the transport_scheme is NULL and the client was set with a custom transport the custom transport will be returned.
*
* Notes:
* - This function should be called only on MQTT_EVENT_BEFORE_CONNECT.
* - The intetion is to provide a way to set different configurations than the ones available from client config.
* - If esp_mqtt_client_destroy is called the returned pointer will be invalidated.
* - All the required settings should be made in the MQTT_EVENT_BEFORE_CONNECT event handler
*
* @param client *MQTT* client handle
* @param transport_scheme Transport handle to search for.
* @return the transport handle
* NULL in case of error
*
*/
esp_transport_handle_t esp_mqtt_client_get_transport(esp_mqtt_client_handle_t client, char *transport_scheme);
#ifdef __cplusplus
}
#endif //__cplusplus
Expand Down
21 changes: 13 additions & 8 deletions mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ static const char *TAG = "mqtt_client";
ESP_EVENT_DEFINE_BASE(MQTT_EVENTS);
#endif

#define MQTT_OVER_TCP_SCHEME "mqtt"
#define MQTT_OVER_SSL_SCHEME "mqtts"
#define MQTT_OVER_WS_SCHEME "ws"
#define MQTT_OVER_WSS_SCHEME "wss"

const static int STOPPED_BIT = (1 << 0);
const static int RECONNECT_BIT = (1 << 1);
const static int DISCONNECT_BIT = (1 << 2);
Expand Down Expand Up @@ -1587,9 +1582,6 @@ static void esp_mqtt_task(void *pv)
break;
case MQTT_STATE_INIT:
xEventGroupClearBits(client->status_bits, RECONNECT_BIT | DISCONNECT_BIT);
client->event.event_id = MQTT_EVENT_BEFORE_CONNECT;
esp_mqtt_dispatch_event_with_msgid(client);


client->transport = client->config->transport;
if (!client->transport) {
Expand Down Expand Up @@ -1617,6 +1609,9 @@ static void esp_mqtt_task(void *pv)
esp_mqtt_set_ssl_transport_properties(client->transport_list, client->config);
#endif

client->event.event_id = MQTT_EVENT_BEFORE_CONNECT;
esp_mqtt_dispatch_event_with_msgid(client);

if (esp_transport_connect(client->transport,
client->config->host,
client->config->port,
Expand Down Expand Up @@ -2282,3 +2277,13 @@ int esp_mqtt_client_get_outbox_size(esp_mqtt_client_handle_t client)

return outbox_size;
}

esp_transport_handle_t esp_mqtt_client_get_transport(esp_mqtt_client_handle_t client, char *transport_scheme){
if (client == NULL || (transport_scheme == NULL && client->config->transport == NULL)) {
return NULL;
}
if (transport_scheme == NULL && client->config->transport != NULL) {
return client->config->transport;
}
return esp_transport_list_get_transport(client->transport_list, transport_scheme);
}

0 comments on commit 1420c01

Please sign in to comment.