Merge branch 'bugfix/mqtt_err_flags_extention' into 'master'

error reporting: extended error event to use errno captured in transports

See merge request espressif/esp-mqtt!80
This commit is contained in:
David Čermák
2020-11-13 05:10:58 +08:00
3 changed files with 22 additions and 8 deletions

View File

@ -80,10 +80,17 @@ typedef enum {
*/ */
typedef enum { typedef enum {
MQTT_ERROR_TYPE_NONE = 0, MQTT_ERROR_TYPE_NONE = 0,
MQTT_ERROR_TYPE_ESP_TLS, MQTT_ERROR_TYPE_TCP_TRANSPORT,
MQTT_ERROR_TYPE_CONNECTION_REFUSED, MQTT_ERROR_TYPE_CONNECTION_REFUSED,
} esp_mqtt_error_type_t; } esp_mqtt_error_type_t;
/**
* MQTT_ERROR_TYPE_TCP_TRANSPORT error type hold all sorts of transport layer errors,
* including ESP-TLS error, but in the past only the errors from MQTT_ERROR_TYPE_ESP_TLS layer
* were reported, so the ESP-TLS error type is re-defined here for backward compatibility
*/
#define MQTT_ERROR_TYPE_ESP_TLS MQTT_ERROR_TYPE_TCP_TRANSPORT
typedef enum { typedef enum {
MQTT_TRANSPORT_UNKNOWN = 0x0, MQTT_TRANSPORT_UNKNOWN = 0x0,
MQTT_TRANSPORT_OVER_TCP, /*!< MQTT over TCP, using scheme: ``mqtt`` */ MQTT_TRANSPORT_OVER_TCP, /*!< MQTT over TCP, using scheme: ``mqtt`` */
@ -110,7 +117,7 @@ typedef enum {
* Use this structure directly checking error_type first and then appropriate error code depending on the source of the error: * Use this structure directly checking error_type first and then appropriate error code depending on the source of the error:
* *
* | error_type | related member variables | note | * | error_type | related member variables | note |
* | MQTT_ERROR_TYPE_ESP_TLS | esp_tls_last_esp_err, esp_tls_stack_err, esp_tls_cert_verify_flags | Error reported from esp-tls | * | MQTT_ERROR_TYPE_TCP_TRANSPORT | esp_tls_last_esp_err, esp_tls_stack_err, esp_tls_cert_verify_flags, sock_errno | Error reported from tcp_transport/esp-tls |
* | MQTT_ERROR_TYPE_CONNECTION_REFUSED | connect_return_code | Internal error reported from MQTT broker on connection | * | MQTT_ERROR_TYPE_CONNECTION_REFUSED | connect_return_code | Internal error reported from MQTT broker on connection |
*/ */
typedef struct esp_mqtt_error_codes { typedef struct esp_mqtt_error_codes {
@ -121,6 +128,9 @@ typedef struct esp_mqtt_error_codes {
/* esp-mqtt specific structure extension */ /* esp-mqtt specific structure extension */
esp_mqtt_error_type_t error_type; /*!< error type referring to the source of the error */ esp_mqtt_error_type_t error_type; /*!< error type referring to the source of the error */
esp_mqtt_connect_return_code_t connect_return_code; /*!< connection refused error code reported from MQTT broker on connection */ esp_mqtt_connect_return_code_t connect_return_code; /*!< connection refused error code reported from MQTT broker on connection */
/* tcp_transport extension */
int esp_transport_sock_errno; /*!< errno from the underlying socket */
} esp_mqtt_error_codes_t; } esp_mqtt_error_codes_t;
/** /**

View File

@ -56,6 +56,7 @@
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 0) #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 0)
// Features supported in 4.3 // Features supported in 4.3
#define MQTT_SUPPORTED_FEATURE_DIGITAL_SIGNATURE #define MQTT_SUPPORTED_FEATURE_DIGITAL_SIGNATURE
#define MQTT_SUPPORTED_FEATURE_TRANSPORT_SOCK_ERRNO_REPORTING
#endif #endif
#endif /* ESP_IDF_VERSION */ #endif /* ESP_IDF_VERSION */

View File

@ -132,7 +132,7 @@ static esp_err_t esp_mqtt_abort_connection(esp_mqtt_client_handle_t client);
static esp_err_t esp_mqtt_client_ping(esp_mqtt_client_handle_t client); static esp_err_t esp_mqtt_client_ping(esp_mqtt_client_handle_t client);
static char *create_string(const char *ptr, int len); static char *create_string(const char *ptr, int len);
static int mqtt_message_receive(esp_mqtt_client_handle_t client, int read_poll_timeout_ms); static int mqtt_message_receive(esp_mqtt_client_handle_t client, int read_poll_timeout_ms);
static void esp_mqtt_client_dispatch_tls_error(esp_mqtt_client_handle_t client); static void esp_mqtt_client_dispatch_transport_error(esp_mqtt_client_handle_t client);
#if MQTT_ENABLE_SSL #if MQTT_ENABLE_SSL
@ -871,7 +871,7 @@ static esp_err_t mqtt_write_data(esp_mqtt_client_handle_t client)
client->config->network_timeout_ms); client->config->network_timeout_ms);
// client->mqtt_state.pending_msg_type = mqtt_get_type(client->mqtt_state.outbound_message->data); // client->mqtt_state.pending_msg_type = mqtt_get_type(client->mqtt_state.outbound_message->data);
if (write_len <= 0) { if (write_len <= 0) {
esp_mqtt_client_dispatch_tls_error(client); esp_mqtt_client_dispatch_transport_error(client);
ESP_LOGE(TAG, "Error write data or timeout, written len = %d, errno=%d", write_len, errno); ESP_LOGE(TAG, "Error write data or timeout, written len = %d, errno=%d", write_len, errno);
return ESP_FAIL; return ESP_FAIL;
} }
@ -1147,7 +1147,7 @@ static int mqtt_message_receive(esp_mqtt_client_handle_t client, int read_poll_t
ESP_LOGD(TAG, "%s: transport_read():%d %d", __func__, client->mqtt_state.in_buffer_read_len, client->mqtt_state.message_length); ESP_LOGD(TAG, "%s: transport_read():%d %d", __func__, client->mqtt_state.in_buffer_read_len, client->mqtt_state.message_length);
return 1; return 1;
err: err:
esp_mqtt_client_dispatch_tls_error(client); esp_mqtt_client_dispatch_transport_error(client);
return -1; return -1;
} }
@ -1324,7 +1324,7 @@ static void esp_mqtt_task(void *pv)
client->config->port, client->config->port,
client->config->network_timeout_ms) < 0) { client->config->network_timeout_ms) < 0) {
ESP_LOGE(TAG, "Error transport connect"); ESP_LOGE(TAG, "Error transport connect");
esp_mqtt_client_dispatch_tls_error(client); esp_mqtt_client_dispatch_transport_error(client);
esp_mqtt_abort_connection(client); esp_mqtt_abort_connection(client);
break; break;
} }
@ -1745,15 +1745,18 @@ esp_err_t esp_mqtt_client_register_event(esp_mqtt_client_handle_t client, esp_mq
} }
static void esp_mqtt_client_dispatch_tls_error(esp_mqtt_client_handle_t client) static void esp_mqtt_client_dispatch_transport_error(esp_mqtt_client_handle_t client)
{ {
client->event.event_id = MQTT_EVENT_ERROR; client->event.event_id = MQTT_EVENT_ERROR;
client->event.error_handle->error_type = MQTT_ERROR_TYPE_ESP_TLS; client->event.error_handle->error_type = MQTT_ERROR_TYPE_TCP_TRANSPORT;
client->event.error_handle->connect_return_code = 0; client->event.error_handle->connect_return_code = 0;
#ifdef MQTT_SUPPORTED_FEATURE_TRANSPORT_ERR_REPORTING #ifdef MQTT_SUPPORTED_FEATURE_TRANSPORT_ERR_REPORTING
client->event.error_handle->esp_tls_last_esp_err = esp_tls_get_and_clear_last_error(esp_transport_get_error_handle(client->transport), client->event.error_handle->esp_tls_last_esp_err = esp_tls_get_and_clear_last_error(esp_transport_get_error_handle(client->transport),
&client->event.error_handle->esp_tls_stack_err, &client->event.error_handle->esp_tls_stack_err,
&client->event.error_handle->esp_tls_cert_verify_flags); &client->event.error_handle->esp_tls_cert_verify_flags);
#ifdef MQTT_SUPPORTED_FEATURE_TRANSPORT_SOCK_ERRNO_REPORTING
client->event.error_handle->esp_transport_sock_errno = esp_transport_get_errno(client->transport);
#endif
#endif #endif
esp_mqtt_dispatch_event_with_msgid(client); esp_mqtt_dispatch_event_with_msgid(client);
} }