Skip to content

Commit 1420c01

Browse files
Merge branch 'feat/get_transport' into 'master'
feat: Allow users to get the transport in use See merge request espressif/esp-mqtt!216
2 parents 72d7bc7 + 97dc85a commit 1420c01

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

include/mqtt_client.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ typedef void *esp_event_handler_t;
2929

3030
typedef struct esp_mqtt_client *esp_mqtt_client_handle_t;
3131

32+
#define MQTT_OVER_TCP_SCHEME "mqtt"
33+
#define MQTT_OVER_SSL_SCHEME "mqtts"
34+
#define MQTT_OVER_WS_SCHEME "ws"
35+
#define MQTT_OVER_WSS_SCHEME "wss"
36+
3237
/**
3338
* @brief *MQTT* event types.
3439
*
@@ -661,6 +666,27 @@ int esp_mqtt_client_get_outbox_size(esp_mqtt_client_handle_t client);
661666
*/
662667
esp_err_t esp_mqtt_dispatch_custom_event(esp_mqtt_client_handle_t client, esp_mqtt_event_t *event);
663668

669+
/**
670+
* @brief Get a transport from the scheme
671+
*
672+
* Allows extra settings to be made on the selected transport,
673+
* for convenience the scheme used by the mqtt client are defined as
674+
* MQTT_OVER_TCP_SCHEME, MQTT_OVER_SSL_SCHEME, MQTT_OVER_WS_SCHEME and MQTT_OVER_WSS_SCHEME
675+
* If the transport_scheme is NULL and the client was set with a custom transport the custom transport will be returned.
676+
*
677+
* Notes:
678+
* - This function should be called only on MQTT_EVENT_BEFORE_CONNECT.
679+
* - The intetion is to provide a way to set different configurations than the ones available from client config.
680+
* - If esp_mqtt_client_destroy is called the returned pointer will be invalidated.
681+
* - All the required settings should be made in the MQTT_EVENT_BEFORE_CONNECT event handler
682+
*
683+
* @param client *MQTT* client handle
684+
* @param transport_scheme Transport handle to search for.
685+
* @return the transport handle
686+
* NULL in case of error
687+
*
688+
*/
689+
esp_transport_handle_t esp_mqtt_client_get_transport(esp_mqtt_client_handle_t client, char *transport_scheme);
664690
#ifdef __cplusplus
665691
}
666692
#endif //__cplusplus

mqtt_client.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ static const char *TAG = "mqtt_client";
2424
ESP_EVENT_DEFINE_BASE(MQTT_EVENTS);
2525
#endif
2626

27-
#define MQTT_OVER_TCP_SCHEME "mqtt"
28-
#define MQTT_OVER_SSL_SCHEME "mqtts"
29-
#define MQTT_OVER_WS_SCHEME "ws"
30-
#define MQTT_OVER_WSS_SCHEME "wss"
31-
3227
const static int STOPPED_BIT = (1 << 0);
3328
const static int RECONNECT_BIT = (1 << 1);
3429
const static int DISCONNECT_BIT = (1 << 2);
@@ -1587,9 +1582,6 @@ static void esp_mqtt_task(void *pv)
15871582
break;
15881583
case MQTT_STATE_INIT:
15891584
xEventGroupClearBits(client->status_bits, RECONNECT_BIT | DISCONNECT_BIT);
1590-
client->event.event_id = MQTT_EVENT_BEFORE_CONNECT;
1591-
esp_mqtt_dispatch_event_with_msgid(client);
1592-
15931585

15941586
client->transport = client->config->transport;
15951587
if (!client->transport) {
@@ -1617,6 +1609,9 @@ static void esp_mqtt_task(void *pv)
16171609
esp_mqtt_set_ssl_transport_properties(client->transport_list, client->config);
16181610
#endif
16191611

1612+
client->event.event_id = MQTT_EVENT_BEFORE_CONNECT;
1613+
esp_mqtt_dispatch_event_with_msgid(client);
1614+
16201615
if (esp_transport_connect(client->transport,
16211616
client->config->host,
16221617
client->config->port,
@@ -2282,3 +2277,13 @@ int esp_mqtt_client_get_outbox_size(esp_mqtt_client_handle_t client)
22822277

22832278
return outbox_size;
22842279
}
2280+
2281+
esp_transport_handle_t esp_mqtt_client_get_transport(esp_mqtt_client_handle_t client, char *transport_scheme){
2282+
if (client == NULL || (transport_scheme == NULL && client->config->transport == NULL)) {
2283+
return NULL;
2284+
}
2285+
if (transport_scheme == NULL && client->config->transport != NULL) {
2286+
return client->config->transport;
2287+
}
2288+
return esp_transport_list_get_transport(client->transport_list, transport_scheme);
2289+
}

0 commit comments

Comments
 (0)