Skip to content

Commit 0089418

Browse files
committed
Add new API to stop mqtt client asynchronous in the background
1 parent 6af4446 commit 0089418

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
@@ -441,7 +441,7 @@ esp_err_t esp_mqtt_client_reconnect(esp_mqtt_client_handle_t client);
441441
esp_err_t esp_mqtt_client_disconnect(esp_mqtt_client_handle_t client);
442442

443443
/**
444-
* @brief Stops *MQTT* client tasks
444+
* @brief Stops *MQTT* client tasks (blocking, waits until stopped)
445445
*
446446
* * Notes:
447447
* - Cannot be called from the *MQTT* event handler
@@ -454,6 +454,33 @@ esp_err_t esp_mqtt_client_disconnect(esp_mqtt_client_handle_t client);
454454
*/
455455
esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client);
456456

457+
/**
458+
* @brief Stops *MQTT* client tasks non-blocking in the background
459+
* Use esp_mqtt_client_is_stopped() to find out if its already
460+
* stopped.
461+
*
462+
* * Notes:
463+
* - Cannot be called from the *MQTT* event handler
464+
*
465+
* @param client *MQTT* client handle
466+
*
467+
* @return ESP_OK on success
468+
* ESP_ERR_INVALID_ARG on wrong initialization
469+
* ESP_FAIL if client is in invalid state
470+
*/
471+
esp_err_t esp_mqtt_client_initiate_stop(esp_mqtt_client_handle_t client);
472+
473+
/**
474+
* @brief Returns if the *MQTT* client has already stopped
475+
* This api should be used after esp_mqtt_client_initiate_stop()
476+
* has been called.
477+
*
478+
* @param client *MQTT* client handle
479+
*
480+
* @return if its stopped or not
481+
*/
482+
bool esp_mqtt_client_is_stopped(esp_mqtt_client_handle_t client);
483+
457484
#ifdef __cplusplus
458485

459486
#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
@@ -1914,6 +1914,14 @@ static esp_err_t send_disconnect_msg(esp_mqtt_client_handle_t client)
19141914
}
19151915

19161916
esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client)
1917+
{
1918+
esp_err_t result = esp_mqtt_client_initiate_stop(client);
1919+
if (result == ESP_OK)
1920+
xEventGroupWaitBits(client->status_bits, STOPPED_BIT, false, true, portMAX_DELAY);
1921+
return result;
1922+
}
1923+
1924+
esp_err_t esp_mqtt_client_initiate_stop(esp_mqtt_client_handle_t client)
19171925
{
19181926
if (!client) {
19191927
ESP_LOGE(TAG, "Client was not initialized");
@@ -1940,7 +1948,7 @@ esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client)
19401948
client->run = false;
19411949
client->state = MQTT_STATE_DISCONNECTED;
19421950
MQTT_API_UNLOCK(client);
1943-
xEventGroupWaitBits(client->status_bits, STOPPED_BIT, false, true, portMAX_DELAY);
1951+
19441952
return ESP_OK;
19451953
} else {
19461954
ESP_LOGW(TAG, "Client asked to stop, but was not started");
@@ -1949,6 +1957,17 @@ esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client)
19491957
}
19501958
}
19511959

1960+
bool esp_mqtt_client_is_stopped(esp_mqtt_client_handle_t client)
1961+
{
1962+
if (!client) {
1963+
ESP_LOGE(TAG, "Client was not initialized");
1964+
return ESP_ERR_INVALID_ARG;
1965+
}
1966+
1967+
EventBits_t bits = xEventGroupClearBits(client->status_bits, 0);
1968+
return bits & STOPPED_BIT;
1969+
}
1970+
19521971
static esp_err_t esp_mqtt_client_ping(esp_mqtt_client_handle_t client)
19531972
{
19541973
mqtt_msg_pingreq(&client->mqtt_state.connection);

0 commit comments

Comments
 (0)