espressif / esp-mqtt

ESP32 mqtt component
Apache License 2.0
603 stars 255 forks source link

ADD: Get the response code from a failing connection. (IDFGH-998) #100

Closed homeit-hq closed 4 years ago

homeit-hq commented 5 years ago

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.

homeit-hq commented 5 years ago

I've also done a patch for the ESP-MQTT_FOR_IDF_3.1 tag.

diff --git i/include/mqtt_client.h w/include/mqtt_client.h
index 766705d..f03edc7 100755
--- i/include/mqtt_client.h
+++ w/include/mqtt_client.h
@@ -30,6 +30,16 @@ typedef enum {
     MQTT_EVENT_DATA,
 } esp_mqtt_event_id_t;

+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 enum {
     MQTT_TRANSPORT_UNKNOWN = 0x0,
     MQTT_TRANSPORT_OVER_TCP,
@@ -49,6 +59,7 @@ typedef struct {
     char *topic;
     int topic_len;
     int msg_id;
+    uint8_t connect_return_code;
 } esp_mqtt_event_t;

 typedef esp_mqtt_event_t* esp_mqtt_event_handle_t;
diff --git i/lib/include/mqtt_msg.h w/lib/include/mqtt_msg.h
index fd04cd0..fa1448c 100644
--- i/lib/include/mqtt_msg.h
+++ w/lib/include/mqtt_msg.h
@@ -58,15 +58,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
 {
diff --git i/mqtt_client.c w/mqtt_client.c
index 8f3c050..e2f8eb2 100644
--- i/mqtt_client.c
+++ w/mqtt_client.c
@@ -66,6 +66,7 @@ struct esp_mqtt_client {
     int wait_timeout_ms;
     int auto_reconnect;
     esp_mqtt_event_t event;
+
     bool run;
     bool wait_for_ping_resp;
     outbox_handle_t outbox;
@@ -226,31 +227,42 @@ static esp_err_t esp_mqtt_connect(esp_mqtt_client_handle_t client, int timeout_m
         return ESP_FAIL;
     }
     connect_rsp_code = mqtt_get_connect_return_code(client->mqtt_state.in_buffer);
-    switch (connect_rsp_code) {
-        case CONNECTION_ACCEPTED:
-            ESP_LOGD(TAG, "Connected");
-            return ESP_OK;
-        case CONNECTION_REFUSE_PROTOCOL:
-            ESP_LOGW(TAG, "Connection refused, bad protocol");
-            return ESP_FAIL;
-        case CONNECTION_REFUSE_SERVER_UNAVAILABLE:
-            ESP_LOGW(TAG, "Connection refused, server unavailable");
-            return ESP_FAIL;
-        case CONNECTION_REFUSE_BAD_USERNAME:
-            ESP_LOGW(TAG, "Connection refused, bad username or password");
-            return ESP_FAIL;
-        case CONNECTION_REFUSE_NOT_AUTHORIZED:
-            ESP_LOGW(TAG, "Connection refused, not authorized");
-            return ESP_FAIL;
-        default:
-            ESP_LOGW(TAG, "Connection refused, Unknow reason");
-            return ESP_FAIL;
+    if(connect_rsp_code == CONNECTION_ACCEPTED)
+    {
+       ESP_LOGD(TAG, "Connected");
+       return ESP_OK;
     }
-    return ESP_OK;
+    return ESP_FAIL;
 }

 static esp_err_t esp_mqtt_abort_connection(esp_mqtt_client_handle_t client)
 {
+   if(client->state == MQTT_STATE_INIT)
+   {
+       esp_err_t connect_rsp_code = mqtt_get_connect_return_code(client->mqtt_state.in_buffer);
+       switch (connect_rsp_code) {
+           case CONNECTION_ACCEPTED:
+               ESP_LOGD(TAG, "Connected");
+               break;
+           case CONNECTION_REFUSE_PROTOCOL:
+               ESP_LOGW(TAG, "Connection refused, bad protocol");
+               break;
+           case CONNECTION_REFUSE_SERVER_UNAVAILABLE:
+               ESP_LOGW(TAG, "Connection refused, server unavailable");
+               break;
+           case CONNECTION_REFUSE_BAD_USERNAME:
+               ESP_LOGW(TAG, "Connection refused, bad username or password");
+               break;
+           case CONNECTION_REFUSE_NOT_AUTHORIZED:
+               ESP_LOGW(TAG, "Connection refused, not authorized");
+               break;
+           default:
+               ESP_LOGW(TAG, "Connection refused, Unknow reason");
+               break;
+       }
+       client->event.connect_return_code = connect_rsp_code;
+   }
+
     transport_close(client->transport);
     client->wait_timeout_ms = MQTT_RECONNECT_TIMEOUT_MS;
     client->reconnect_tick = platform_tick_get_ms();
homeit-hq commented 5 years ago

This could resolve issue #99 and #94

david-cermak commented 5 years ago

Hi @homeit-hq ,

Thank you for the contribution to the project and for your help with addressing the recent outstanding issues! Let me have some comments to the code, though.

homeit-hq commented 5 years ago

@david-cermak sorry about this change in the PR. I'm fixing another problem that i found, and I worked on top of this change, but was expecting that it would create a new PR and not append to this one.

How do you what me to proceed?

david-cermak commented 5 years ago

@homeit-hq it looks like you've already created a new PR.

As for this PR, If you don't mind I would cherry-pick your commits and modify them to fit the upcoming update. Already prepared a solution to pass error codes to upper layers, this however covers mostly other components in idf (plus it changes api so comes to 4.0), but your changes would be useful to integrate after that. If it's ok with you please keep this open and I will close it upon integration.

homeit-hq commented 5 years ago

@david-cermak fine by me!

david-cermak commented 4 years ago

@homeit-hq Thanks for sharing this fix and apologies that it took so long. Have cherry-picked as 67042a1315c63cf0415f2eb19dca16df1372a6e7.