Merge branch 'bugfix/set_error_on_subscribe_failure' into 'master'

Adds error code to MQTT_EVENT_SUBSCRIBED in case of failure

See merge request espressif/esp-mqtt!143
This commit is contained in:
David Čermák
2022-10-21 23:46:02 +08:00
2 changed files with 49 additions and 37 deletions

View File

@@ -46,12 +46,12 @@ typedef enum esp_mqtt_event_id_t {
MQTT_EVENT_DISCONNECTED, /*!< disconnected event */ MQTT_EVENT_DISCONNECTED, /*!< disconnected event */
MQTT_EVENT_SUBSCRIBED, /*!< subscribed event, additional context: MQTT_EVENT_SUBSCRIBED, /*!< subscribed event, additional context:
- msg_id message id - msg_id message id
- data pointer to the received - error_handle `error_type` in case subscribing failed
data - data pointer to broker response, check for errors.
- data_len length of the data for this - data_len length of the data for this
event event
*/ */
MQTT_EVENT_UNSUBSCRIBED, /*!< unsubscribed event */ MQTT_EVENT_UNSUBSCRIBED, /*!< unsubscribed event, additional context: msg_id */
MQTT_EVENT_PUBLISHED, /*!< published event, additional context: msg_id */ MQTT_EVENT_PUBLISHED, /*!< published event, additional context: msg_id */
MQTT_EVENT_DATA, /*!< data event, additional context: MQTT_EVENT_DATA, /*!< data event, additional context:
- msg_id message id - msg_id message id
@@ -112,6 +112,7 @@ typedef enum esp_mqtt_error_type_t {
MQTT_ERROR_TYPE_NONE = 0, MQTT_ERROR_TYPE_NONE = 0,
MQTT_ERROR_TYPE_TCP_TRANSPORT, MQTT_ERROR_TYPE_TCP_TRANSPORT,
MQTT_ERROR_TYPE_CONNECTION_REFUSED, MQTT_ERROR_TYPE_CONNECTION_REFUSED,
MQTT_ERROR_TYPE_SUBSCRIBE_FAILED
} esp_mqtt_error_type_t; } esp_mqtt_error_type_t;
/** /**

View File

@@ -1,5 +1,7 @@
#include "esp_log.h" #include "mqtt_client.h"
#include "mqtt_client_priv.h" #include "mqtt_client_priv.h"
#include "esp_log.h"
#include <stdint.h>
_Static_assert(sizeof(uint64_t) == sizeof(outbox_tick_t), "mqtt-client tick type size different from outbox tick type"); _Static_assert(sizeof(uint64_t) == sizeof(outbox_tick_t), "mqtt-client tick type size different from outbox tick type");
#ifdef ESP_EVENT_ANY_ID #ifdef ESP_EVENT_ANY_ID
@@ -588,7 +590,8 @@ void esp_mqtt_destroy_config(esp_mqtt_client_handle_t client)
client->config = NULL; client->config = NULL;
} }
static inline bool has_timed_out(uint64_t last_tick, uint64_t timeout) { static inline bool has_timed_out(uint64_t last_tick, uint64_t timeout)
{
uint64_t next = last_tick + timeout; uint64_t next = last_tick + timeout;
return (int64_t)(next - platform_tick_get_ms()) <= 0; return (int64_t)(next - platform_tick_get_ms()) <= 0;
} }
@@ -608,7 +611,7 @@ static esp_err_t process_keepalive(esp_mqtt_client_handle_t client)
return ESP_OK; return ESP_OK;
} }
if (has_timed_out(client->keepalive_tick, keepalive_ms/2)) { if (has_timed_out(client->keepalive_tick, keepalive_ms / 2)) {
if (esp_mqtt_client_ping(client) == ESP_FAIL) { if (esp_mqtt_client_ping(client) == ESP_FAIL) {
ESP_LOGE(TAG, "Can't send ping, disconnected"); ESP_LOGE(TAG, "Can't send ping, disconnected");
esp_mqtt_abort_connection(client); esp_mqtt_abort_connection(client);
@@ -1079,7 +1082,15 @@ static esp_err_t deliver_suback(esp_mqtt_client_handle_t client)
ESP_LOGE(TAG, "Failed to acquire suback data"); ESP_LOGE(TAG, "Failed to acquire suback data");
return ESP_FAIL; return ESP_FAIL;
} }
client->event.error_handle->esp_tls_stack_err = 0;
client->event.error_handle->esp_tls_last_esp_err = 0;
client->event.error_handle->esp_tls_cert_verify_flags = 0;
client->event.error_handle->error_type = MQTT_ERROR_TYPE_NONE;
client->event.error_handle->connect_return_code = MQTT_CONNECTION_ACCEPTED;
// post data event // post data event
if ((uint8_t)*msg_data >= 0x80) {
client->event.error_handle->error_type = MQTT_ERROR_TYPE_SUBSCRIBE_FAILED;
}
client->event.data_len = msg_data_len; client->event.data_len = msg_data_len;
client->event.total_data_len = msg_data_len; client->event.total_data_len = msg_data_len;
client->event.event_id = MQTT_EVENT_SUBSCRIBED; client->event.event_id = MQTT_EVENT_SUBSCRIBED;
@@ -1425,7 +1436,7 @@ static esp_err_t mqtt_resend_queued(esp_mqtt_client_handle_t client, outbox_item
// set duplicate flag for QoS-1 and QoS-2 messages // set duplicate flag for QoS-1 and QoS-2 messages
if (client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_PUBLISH && client->mqtt_state.pending_publish_qos > 0 && (outbox_item_get_pending(item) == TRANSMITTED)) { if (client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_PUBLISH && client->mqtt_state.pending_publish_qos > 0 && (outbox_item_get_pending(item) == TRANSMITTED)) {
mqtt_set_dup(client->mqtt_state.outbound_message->data); mqtt_set_dup(client->mqtt_state.outbound_message->data);
ESP_LOGD(TAG,"Sending Duplicated QoS%d message with id=%d", client->mqtt_state.pending_publish_qos, client->mqtt_state.pending_msg_id); ESP_LOGD(TAG, "Sending Duplicated QoS%d message with id=%d", client->mqtt_state.pending_publish_qos, client->mqtt_state.pending_msg_id);
} }
// try to resend the data // try to resend the data
@@ -1477,7 +1488,7 @@ static inline int max_poll_timeout(esp_mqtt_client_handle_t client, int max_time
{ {
return return
#if MQTT_EVENT_QUEUE_SIZE > 1 #if MQTT_EVENT_QUEUE_SIZE > 1
atomic_load(&client->queued_events) > 0 ? 10: max_timeout; atomic_load(&client->queued_events) > 0 ? 10 : max_timeout;
#else #else
max_timeout; max_timeout;
#endif #endif