Skip to content

Commit 1399d36

Browse files
committed
Add new API to stop mqtt client asynchronous in the background
1 parent cac1552 commit 1399d36

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

include/mqtt_client.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ esp_err_t esp_mqtt_client_reconnect(esp_mqtt_client_handle_t client);
428428
esp_err_t esp_mqtt_client_disconnect(esp_mqtt_client_handle_t client);
429429

430430
/**
431-
* @brief Stops *MQTT* client tasks
431+
* @brief Stops *MQTT* client tasks (blocking, waits until stopped)
432432
*
433433
* * Notes:
434434
* - Cannot be called from the *MQTT* event handler
@@ -441,6 +441,33 @@ esp_err_t esp_mqtt_client_disconnect(esp_mqtt_client_handle_t client);
441441
*/
442442
esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client);
443443

444+
/**
445+
* @brief Stops *MQTT* client tasks non-blocking in the background
446+
* Use esp_mqtt_client_is_stopped() to find out if its already
447+
* stopped.
448+
*
449+
* * Notes:
450+
* - Cannot be called from the *MQTT* event handler
451+
*
452+
* @param client *MQTT* client handle
453+
*
454+
* @return ESP_OK on success
455+
* ESP_ERR_INVALID_ARG on wrong initialization
456+
* ESP_FAIL if client is in invalid state
457+
*/
458+
esp_err_t esp_mqtt_client_initiate_stop(esp_mqtt_client_handle_t client);
459+
460+
/**
461+
* @brief Returns if the *MQTT* client has already stopped
462+
* This api should be used after esp_mqtt_client_initiate_stop()
463+
* has been called.
464+
*
465+
* @param client *MQTT* client handle
466+
*
467+
* @return if its stopped or not
468+
*/
469+
bool esp_mqtt_client_is_stopped(esp_mqtt_client_handle_t client);
470+
444471
#ifdef __cplusplus
445472

446473
#define esp_mqtt_client_subscribe esp_mqtt_client_subscribe_single

mqtt_client.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,14 @@ static esp_err_t send_disconnect_msg(esp_mqtt_client_handle_t client)
18311831
}
18321832

18331833
esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client)
1834+
{
1835+
esp_err_t result = esp_mqtt_client_initiate_stop(client);
1836+
if (result == ESP_OK)
1837+
xEventGroupWaitBits(client->status_bits, STOPPED_BIT, false, true, portMAX_DELAY);
1838+
return result;
1839+
}
1840+
1841+
esp_err_t esp_mqtt_client_initiate_stop(esp_mqtt_client_handle_t client)
18341842
{
18351843
if (!client) {
18361844
ESP_LOGE(TAG, "Client was not initialized");
@@ -1857,7 +1865,7 @@ esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client)
18571865
client->run = false;
18581866
client->state = MQTT_STATE_DISCONNECTED;
18591867
MQTT_API_UNLOCK(client);
1860-
xEventGroupWaitBits(client->status_bits, STOPPED_BIT, false, true, portMAX_DELAY);
1868+
18611869
return ESP_OK;
18621870
} else {
18631871
ESP_LOGW(TAG, "Client asked to stop, but was not started");
@@ -1866,6 +1874,17 @@ esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client)
18661874
}
18671875
}
18681876

1877+
bool esp_mqtt_client_is_stopped(esp_mqtt_client_handle_t client)
1878+
{
1879+
if (!client) {
1880+
ESP_LOGE(TAG, "Client was not initialized");
1881+
return ESP_ERR_INVALID_ARG;
1882+
}
1883+
1884+
EventBits_t bits = xEventGroupClearBits(client->status_bits, 0);
1885+
return bits & STOPPED_BIT;
1886+
}
1887+
18691888
static esp_err_t esp_mqtt_client_ping(esp_mqtt_client_handle_t client)
18701889
{
18711890
mqtt_msg_pingreq(&client->mqtt_state.connection);

0 commit comments

Comments
 (0)