Skip to content

Commit

Permalink
Merge branch 'bugfix/send_diconnect_msg' into 'master'
Browse files Browse the repository at this point in the history
Client: Disconnect/Reconnect improvements

See merge request espressif/esp-mqtt!113
  • Loading branch information
david-cermak committed Oct 21, 2021
2 parents 5ec3702 + 320b058 commit 36de30e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 71 deletions.
62 changes: 5 additions & 57 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
stages:
- build
- static_analysis
- deploy_report
- deploy


Expand Down Expand Up @@ -36,10 +34,6 @@ build_with_idf_v3:
- $MQTT_PATH/ci/set_idf.sh release/v3.2
- $MQTT_PATH/ci/set_mqtt.sh $CI_COMMIT_SHA
- $MQTT_PATH/ci/build_examples.sh make
# build with IDFv3.3
- $MQTT_PATH/ci/set_idf.sh release/v3.3
- $MQTT_PATH/ci/set_mqtt.sh $CI_COMMIT_SHA
- $MQTT_PATH/ci/build_examples.sh make
# simple build with IDFv3.1 (buiding directly in mqtt repo)
- $MQTT_PATH/ci/set_idf.sh release/v3.1
- cd $MQTT_PATH && ./ci/modify_for_legacy_idf.sh ${RECENT_IDF} || true
Expand Down Expand Up @@ -95,6 +89,11 @@ build_with_idf_v4:
- cd $IDF_PATH && tools/idf_tools.py --non-interactive install && eval "$(tools/idf_tools.py --non-interactive export)"
- $MQTT_PATH/ci/set_mqtt.sh $CI_COMMIT_SHA
- $MQTT_PATH/ci/build_examples.sh
# build with IDFv3.3
- $MQTT_PATH/ci/set_idf.sh release/v3.3
- cd $IDF_PATH && tools/idf_tools.py --non-interactive install && eval "$(tools/idf_tools.py --non-interactive export)"
- $MQTT_PATH/ci/set_mqtt.sh $CI_COMMIT_SHA
- $MQTT_PATH/ci/build_examples.sh make

build_and_test_qemu:
stage: build
Expand All @@ -121,57 +120,6 @@ build_and_test_qemu:
- cd $IDF_PATH/tools/ci/python_packages/tiny_test_fw/bin
- python Runner.py $TEST_PATH -c $TEST_PATH/publish_connect_mqtt_qemu.yml -e $TEST_PATH/env.yml

clang_tidy_check:
stage: static_analysis
image: ${CI_DOCKER_REGISTRY}/clang-static-analysis
tags:
- host_test
dependencies:
- build_with_idf_v4
artifacts:
reports:
junit: esp-idf/examples/protocols/mqtt/tcp/tidybuild/output.xml
when: always
paths:
- esp-idf/examples/protocols/mqtt/tcp/tidybuild/report/*
expire_in: 1 day
script:
- cit_add_ssh_key "${GITLAB_KEY}"
- git clone "${IDF_REPO}"
- cd esp-idf
- ./tools/ci/mirror-submodule-update.sh
- export IDF_PATH=$(pwd)
- cd $IDF_PATH/components/mqtt/esp-mqtt
- rm -rf .git
- cp -r $CI_PROJECT_DIR/.git .
- git reset --hard $CI_COMMIT_SHA
- mv $CI_PROJECT_DIR/tidybuild $IDF_PATH/examples/protocols/mqtt/tcp/tidybuild
- cd $IDF_PATH/examples/protocols/mqtt/tcp/tidybuild
- git clone $IDF_ANALYSIS_UTILS static_analysis_utils && cd static_analysis_utils
- ./generate_report.sh $CI_PROJECT_DIR/static-analysis-rules.yml $IDF_PATH/examples/protocols/mqtt/tcp/tidybuild/report $IDF_PATH/examples/protocols/mqtt/tcp/tidybuild/output.xml

deploy_report:
stage: deploy_report
image: $CI_DOCKER_REGISTRY/esp32-ci-env
tags:
- deploy
- shiny
dependencies:
- clang_tidy_check
script:
- cit_add_ssh_key "${DOCS_DEPLOY_KEY}"
- echo -e "Host $DOCS_SERVER\n\tStrictHostKeyChecking no\n\tUser $DOCS_SERVER_USER\n" >> ~/.ssh/config
- export GIT_VER=$(git describe --always)
- cd esp-idf/examples/protocols/mqtt/tcp/tidybuild
- mv report $GIT_VER
- tar czvf $GIT_VER.tar.gz $GIT_VER
- ssh $DOCS_SERVER -x "mkdir -p $DOCS_PATH/clang-tidy"
- scp $GIT_VER.tar.gz $DOCS_SERVER:$DOCS_PATH/clang-tidy
- ssh $DOCS_SERVER -x "cd $DOCS_PATH/clang-tidy && tar xzvf $GIT_VER.tar.gz && rm -f latest && ln -s $GIT_VER latest"
# add link to view the report
- echo "[static analysis][clang tidy] $CI_DOCKER_REGISTRY/static_analysis/esp-idf/clang-tidy/${GIT_VER}/index.html"
- test ! -e ${GIT_VER}/FAILED_RULES || { echo 'Failed static analysis rules!'; cat ${GIT_VER}/FAILED_RULES; exit 1; }

push_master_to_github:
stage: deploy
image: ${CI_DOCKER_REGISTRY}/esp32-ci-env
Expand Down
38 changes: 24 additions & 14 deletions mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static esp_err_t esp_mqtt_client_ping(esp_mqtt_client_handle_t client);
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 void esp_mqtt_client_dispatch_transport_error(esp_mqtt_client_handle_t client);

static esp_err_t send_disconnect_msg(esp_mqtt_client_handle_t client);

#if MQTT_ENABLE_SSL
enum esp_mqtt_ssl_cert_key_api {
Expand Down Expand Up @@ -1451,6 +1451,7 @@ static void esp_mqtt_task(void *pv)
case MQTT_STATE_CONNECTED:
// check for disconnection request
if (xEventGroupWaitBits(client->status_bits, DISCONNECT_BIT, true, true, 0) & DISCONNECT_BIT) {
send_disconnect_msg(client); // ignore error, if clean disconnect fails, just abort the connection
esp_mqtt_abort_connection(client);
break;
}
Expand Down Expand Up @@ -1507,13 +1508,14 @@ static void esp_mqtt_task(void *pv)
break;
case MQTT_STATE_WAIT_RECONNECT:

if (!client->config->auto_reconnect) {
client->run = false;
client->state = MQTT_STATE_DISCONNECTED;
ESP_LOGD(TAG, "MQTT client disconnected.");
if (!client->config->auto_reconnect && xEventGroupGetBits(client->status_bits)&RECONNECT_BIT) {
xEventGroupClearBits(client->status_bits, RECONNECT_BIT);
client->state = MQTT_STATE_INIT;
client->wait_timeout_ms = MQTT_RECON_DEFAULT_MS;
ESP_LOGD(TAG, "Reconnecting per user request...");
break;
}
if (platform_tick_get_ms() - client->reconnect_tick > client->wait_timeout_ms) {
} else if (client->config->auto_reconnect &&
platform_tick_get_ms() - client->reconnect_tick > client->wait_timeout_ms) {
client->state = MQTT_STATE_INIT;
client->reconnect_tick = platform_tick_get_ms();
ESP_LOGD(TAG, "Reconnecting...");
Expand Down Expand Up @@ -1601,6 +1603,20 @@ esp_err_t esp_mqtt_client_reconnect(esp_mqtt_client_handle_t client)
return ESP_OK;
}

static esp_err_t send_disconnect_msg(esp_mqtt_client_handle_t client)
{
// Notify the broker we are disconnecting
client->mqtt_state.outbound_message = mqtt_msg_disconnect(&client->mqtt_state.mqtt_connection);
if (client->mqtt_state.outbound_message->length == 0) {
ESP_LOGE(TAG, "Disconnect message cannot be created");
return ESP_FAIL;
}
if (mqtt_write_data(client) != ESP_OK) {
ESP_LOGE(TAG, "Error sending disconnect message");
}
return ESP_OK;
}

esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client)
{
if (!client) {
Expand All @@ -1619,16 +1635,10 @@ esp_err_t esp_mqtt_client_stop(esp_mqtt_client_handle_t client)

// Only send the disconnect message if the client is connected
if (client->state == MQTT_STATE_CONNECTED) {
// Notify the broker we are disconnecting
client->mqtt_state.outbound_message = mqtt_msg_disconnect(&client->mqtt_state.mqtt_connection);
if (client->mqtt_state.outbound_message->length == 0) {
ESP_LOGE(TAG, "Disconnect message cannot be created");
if (send_disconnect_msg(client) != ESP_OK) {
MQTT_API_UNLOCK(client);
return ESP_FAIL;
}
if (mqtt_write_data(client) != ESP_OK) {
ESP_LOGE(TAG, "Error sending disconnect message");
}
}

client->run = false;
Expand Down

0 comments on commit 36de30e

Please sign in to comment.