mirror of
https://github.com/espressif/esp-mqtt.git
synced 2025-08-03 12:45:34 +02:00
fixed dis/re-connect when internet connection fails
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#ifndef _MQTT_H_
|
#ifndef _MQTT_H_
|
||||||
#define _MQTT_H_
|
#define _MQTT_H_
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "mqtt_config.h"
|
#include "mqtt_config.h"
|
||||||
#include "mqtt_msg.h"
|
#include "mqtt_msg.h"
|
||||||
|
40
mqtt.c
40
mqtt.c
@@ -150,6 +150,13 @@ static bool client_connect(mqtt_client *client)
|
|||||||
// including SSL objects if CNFIG_MQTT_SECURITY_ON is enabled
|
// including SSL objects if CNFIG_MQTT_SECURITY_ON is enabled
|
||||||
void closeclient(mqtt_client *client)
|
void closeclient(mqtt_client *client)
|
||||||
{
|
{
|
||||||
|
mqtt_info("Closing client socket");
|
||||||
|
|
||||||
|
if (client->socket != -1)
|
||||||
|
{
|
||||||
|
close(client->socket);
|
||||||
|
client->socket = -1;
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(CONFIG_MQTT_SECURITY_ON)
|
#if defined(CONFIG_MQTT_SECURITY_ON)
|
||||||
if (client->ssl != NULL)
|
if (client->ssl != NULL)
|
||||||
@@ -159,14 +166,7 @@ void closeclient(mqtt_client *client)
|
|||||||
SSL_free(client->ssl);
|
SSL_free(client->ssl);
|
||||||
client->ssl = NULL;
|
client->ssl = NULL;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
if (client->socket != -1)
|
|
||||||
{
|
|
||||||
close(client->socket);
|
|
||||||
client->socket = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(CONFIG_MQTT_SECURITY_ON)
|
|
||||||
if (client->ctx != NULL)
|
if (client->ctx != NULL)
|
||||||
{
|
{
|
||||||
SSL_CTX_free(client->ctx);
|
SSL_CTX_free(client->ctx);
|
||||||
@@ -305,9 +305,10 @@ void mqtt_sending_task(void *pvParameters)
|
|||||||
mqtt_client *client = (mqtt_client *)pvParameters;
|
mqtt_client *client = (mqtt_client *)pvParameters;
|
||||||
uint32_t msg_len;
|
uint32_t msg_len;
|
||||||
int send_len;
|
int send_len;
|
||||||
|
bool connected = true;
|
||||||
mqtt_info("mqtt_sending_task");
|
mqtt_info("mqtt_sending_task");
|
||||||
|
|
||||||
while (1) {
|
while (connected) {
|
||||||
if (xQueueReceive(client->xSendingQueue, &msg_len, 1000 / portTICK_RATE_MS)) {
|
if (xQueueReceive(client->xSendingQueue, &msg_len, 1000 / portTICK_RATE_MS)) {
|
||||||
//queue available
|
//queue available
|
||||||
while (msg_len > 0) {
|
while (msg_len > 0) {
|
||||||
@@ -319,10 +320,11 @@ void mqtt_sending_task(void *pvParameters)
|
|||||||
rb_read(&client->send_rb, client->mqtt_state.out_buffer, send_len);
|
rb_read(&client->send_rb, client->mqtt_state.out_buffer, send_len);
|
||||||
client->mqtt_state.pending_msg_type = mqtt_get_type(client->mqtt_state.out_buffer);
|
client->mqtt_state.pending_msg_type = mqtt_get_type(client->mqtt_state.out_buffer);
|
||||||
client->mqtt_state.pending_msg_id = mqtt_get_id(client->mqtt_state.out_buffer, send_len);
|
client->mqtt_state.pending_msg_id = mqtt_get_id(client->mqtt_state.out_buffer, send_len);
|
||||||
send_len = client->settings->write_cb(client, client->mqtt_state.out_buffer, send_len, 0);
|
send_len = client->settings->write_cb(client, client->mqtt_state.out_buffer, send_len, 5 * 1000);
|
||||||
if(send_len < 0) {
|
if(send_len <= 0) {
|
||||||
mqtt_info("Write error: %d", errno);
|
mqtt_info("Write error: %d", errno);
|
||||||
break; // TODO is this right handling?
|
connected = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Check sending type, to callback publish message
|
//TODO: Check sending type, to callback publish message
|
||||||
@@ -340,12 +342,19 @@ void mqtt_sending_task(void *pvParameters)
|
|||||||
client->mqtt_state.pending_msg_id = mqtt_get_id(client->mqtt_state.outbound_message->data,
|
client->mqtt_state.pending_msg_id = mqtt_get_id(client->mqtt_state.outbound_message->data,
|
||||||
client->mqtt_state.outbound_message->length);
|
client->mqtt_state.outbound_message->length);
|
||||||
mqtt_info("Sending pingreq");
|
mqtt_info("Sending pingreq");
|
||||||
client->settings->write_cb(client,
|
send_len = client->settings->write_cb(client,
|
||||||
client->mqtt_state.outbound_message->data,
|
client->mqtt_state.outbound_message->data,
|
||||||
client->mqtt_state.outbound_message->length, 0);
|
client->mqtt_state.outbound_message->length, 0);
|
||||||
|
if(send_len <= 0) {
|
||||||
|
mqtt_info("Write error: %d", errno);
|
||||||
|
connected = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
closeclient(client);
|
||||||
|
xMqttSendingTask = NULL;
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -399,13 +408,12 @@ void mqtt_start_receive_schedule(mqtt_client *client)
|
|||||||
while (1) {
|
while (1) {
|
||||||
|
|
||||||
if (terminate_mqtt) break;
|
if (terminate_mqtt) break;
|
||||||
|
if (xMqttSendingTask == NULL) break;
|
||||||
|
|
||||||
read_len = client->settings->read_cb(client, client->mqtt_state.in_buffer, CONFIG_MQTT_BUFFER_SIZE_BYTE, 0);
|
read_len = client->settings->read_cb(client, client->mqtt_state.in_buffer, CONFIG_MQTT_BUFFER_SIZE_BYTE, 0);
|
||||||
|
|
||||||
mqtt_info("Read len %d", read_len);
|
mqtt_info("Read len %d", read_len);
|
||||||
if (read_len == 0)
|
if (read_len <= 0) {
|
||||||
break;
|
|
||||||
if (read_len < 0) {
|
|
||||||
// ECONNRESET for example
|
// ECONNRESET for example
|
||||||
mqtt_info("Read error %d", errno);
|
mqtt_info("Read error %d", errno);
|
||||||
break;
|
break;
|
||||||
@@ -534,7 +542,9 @@ void mqtt_task(void *pvParameters)
|
|||||||
client->settings->disconnected_cb(client, NULL);
|
client->settings->disconnected_cb(client, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (xMqttSendingTask != NULL) {
|
||||||
vTaskDelete(xMqttSendingTask);
|
vTaskDelete(xMqttSendingTask);
|
||||||
|
}
|
||||||
if (!client->settings->auto_reconnect) {
|
if (!client->settings->auto_reconnect) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user