From 67042a1315c63cf0415f2eb19dca16df1372a6e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Cabral?= Date: Fri, 1 Mar 2019 18:20:53 +0000 Subject: [PATCH] ADD: Get the response code from a failing connection. This commit adds an event information "connect_return_code" that is written when the client state is MQTT_STATE_INIT, and the connection fails. This event info can then be written by the user app to find out the reason of the fail connection. Merges https://github.com/espressif/esp-mqtt/pull/100 --- include/mqtt_client.h | 16 +++++++++++++++- lib/include/mqtt_msg.h | 9 --------- mqtt_client.c | 30 ++++++++++++++++++------------ 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/include/mqtt_client.h b/include/mqtt_client.h index b29255e..d08c555 100644 --- a/include/mqtt_client.h +++ b/include/mqtt_client.h @@ -37,7 +37,7 @@ typedef struct esp_mqtt_client *esp_mqtt_client_handle_t; * */ typedef enum { - MQTT_EVENT_ERROR = 0, + MQTT_EVENT_ERROR = 0, /*!< on error event, additional context: error handle from esp_tls (if supported) */ MQTT_EVENT_CONNECTED, /*!< connected event, additional context: session_present flag */ MQTT_EVENT_DISCONNECTED, /*!< disconnected event */ MQTT_EVENT_SUBSCRIBED, /*!< subscribed event, additional context: msg_id */ @@ -57,8 +57,21 @@ typedef enum { and current data offset updating. */ MQTT_EVENT_BEFORE_CONNECT, /*!< The event occurs before connecting */ + MQTT_EVENT_CONNECTION_REFUSED, /*!< The event occurs if connection is refused by the broker, additional context: connect_return_code */ } esp_mqtt_event_id_t; +/** + * MQTT connection error codes propagated via ERROR event + */ +typedef enum { + MQTT_CONNECTION_ACCEPTED = 0, /*!< Connection accepted */ + MQTT_CONNECTION_REFUSE_PROTOCOL, /*!< MQTT connection refused reason: Wrong protocol */ + MQTT_CONNECTION_REFUSE_ID_REJECTED, /*!< MQTT connection refused reason: ID rejected */ + MQTT_CONNECTION_REFUSE_SERVER_UNAVAILABLE, /*!< MQTT connection refused reason: Server unavailable */ + MQTT_CONNECTION_REFUSE_BAD_USERNAME, /*!< MQTT connection refused reason: Wrong user */ + MQTT_CONNECTION_REFUSE_NOT_AUTHORIZED /*!< MQTT connection refused reason: Wrong username or password */ +} esp_mqtt_connect_return_code_t; + typedef enum { MQTT_TRANSPORT_UNKNOWN = 0x0, MQTT_TRANSPORT_OVER_TCP, /*!< MQTT over TCP, using scheme: ``mqtt`` */ @@ -83,6 +96,7 @@ typedef struct { int msg_id; /*!< MQTT messaged id of message */ int session_present; /*!< MQTT session_present flag for connection event */ void* error_handle; /*!< esp-tls error handle referencing last error/flags captured in transports */ + esp_mqtt_connect_return_code_t connect_return_code; /*!< MQTT connection return code. Only written on a connection */ } esp_mqtt_event_t; typedef esp_mqtt_event_t *esp_mqtt_event_handle_t; diff --git a/lib/include/mqtt_msg.h b/lib/include/mqtt_msg.h index 80ab72f..470f4c4 100644 --- a/lib/include/mqtt_msg.h +++ b/lib/include/mqtt_msg.h @@ -57,15 +57,6 @@ enum mqtt_message_type { MQTT_MSG_TYPE_DISCONNECT = 14 }; -enum mqtt_connect_return_code { - CONNECTION_ACCEPTED = 0, - CONNECTION_REFUSE_PROTOCOL, - CONNECTION_REFUSE_ID_REJECTED, - CONNECTION_REFUSE_SERVER_UNAVAILABLE, - CONNECTION_REFUSE_BAD_USERNAME, - CONNECTION_REFUSE_NOT_AUTHORIZED -}; - typedef struct mqtt_message { uint8_t *data; uint32_t length; diff --git a/mqtt_client.c b/mqtt_client.c index f359766..e7ccc2c 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -317,27 +317,33 @@ static esp_err_t esp_mqtt_connect(esp_mqtt_client_handle_t client, int timeout_m } client->mqtt_state.in_buffer_read_len = 0; connect_rsp_code = mqtt_get_connect_return_code(client->mqtt_state.in_buffer); - switch (connect_rsp_code) { - case CONNECTION_ACCEPTED: + if (connect_rsp_code == MQTT_CONNECTION_ACCEPTED) { ESP_LOGD(TAG, "Connected"); return ESP_OK; - case CONNECTION_REFUSE_PROTOCOL: + } + switch (connect_rsp_code) { + case MQTT_CONNECTION_REFUSE_PROTOCOL: ESP_LOGW(TAG, "Connection refused, bad protocol"); - return ESP_FAIL; - case CONNECTION_REFUSE_SERVER_UNAVAILABLE: + break; + case MQTT_CONNECTION_REFUSE_SERVER_UNAVAILABLE: ESP_LOGW(TAG, "Connection refused, server unavailable"); - return ESP_FAIL; - case CONNECTION_REFUSE_BAD_USERNAME: + break; + case MQTT_CONNECTION_REFUSE_BAD_USERNAME: ESP_LOGW(TAG, "Connection refused, bad username or password"); - return ESP_FAIL; - case CONNECTION_REFUSE_NOT_AUTHORIZED: + break; + case MQTT_CONNECTION_REFUSE_NOT_AUTHORIZED: ESP_LOGW(TAG, "Connection refused, not authorized"); - return ESP_FAIL; + break; default: ESP_LOGW(TAG, "Connection refused, Unknow reason"); - return ESP_FAIL; + break; } - return ESP_OK; + /* propagate event with connection refused error */ + client->event.event_id = MQTT_EVENT_CONNECTION_REFUSED; + client->event.connect_return_code = connect_rsp_code; + esp_mqtt_dispatch_event_with_msgid(client); + + return ESP_FAIL; } static esp_err_t esp_mqtt_abort_connection(esp_mqtt_client_handle_t client)