netutils/mqtt: handle exception case in mqtt_disconnect()
This patch handles the exception in which mqtt_disconnect() returns
MOSQ_ERR_NO_CONN even if mqtt_client's state is not 'NOT_CONNECTED'.
If this exception case occurs, mqtt_disconnect() returns the success
value after the state is changed to 'MQTT_CLIENT_STATE_NOT_CONNECTED'.
@@ -466,8 +466,15 @@ int mqtt_disconnect(mqtt_client_t *handle)
handle->state = MQTT_CLIENT_STATE_DISCONNECT_REQUEST;
ret = mosquitto_disconnect(mosq);
if (ret != 0) {
- ndbg("ERROR: mosquitto_disconnect() failed.\n");
+ ndbg("ERROR: mosquitto_disconnect() failed. (ret=%d)\n", ret);
+ if (ret == MOSQ_ERR_NO_CONN) {
+ ndbg("the return value of mosquitto_disconnect() is MOSQ_ERR_NO_CONN.\n");
+ handle->state = MQTT_CLIENT_STATE_NOT_CONNECTED;
+ result = 0;
+ } else {
handle->state = prev_state;
+ }
+
goto done;
}
