Skip to content

Commit f38a5fc

Browse files
fix: pubrel message resending when pubcomp not received
For the case when pubcomp wasn't received we should retry sending pubrel.
1 parent 198d44c commit f38a5fc

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

mqtt_client.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,32 @@ static esp_err_t mqtt_resend_queued(esp_mqtt_client_handle_t client, outbox_item
15501550
return ESP_OK;
15511551
}
15521552

1553+
static esp_err_t mqtt_resend_pubrel(esp_mqtt_client_handle_t client, outbox_item_handle_t item)
1554+
{
1555+
client->mqtt_state.connection.outbound_message.data = outbox_item_get_data(item, &client->mqtt_state.connection.outbound_message.length, &client->mqtt_state.pending_msg_id,
1556+
&client->mqtt_state.pending_msg_type, &client->mqtt_state.pending_publish_qos);
1557+
if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) {
1558+
#ifdef MQTT_PROTOCOL_5
1559+
ESP_LOGI(TAG, "MQTT_MSG_TYPE_PUBREC return code is %d", mqtt5_msg_get_reason_code(client->mqtt_state.in_buffer, client->mqtt_state.in_buffer_read_len));
1560+
mqtt5_msg_pubrel(&client->mqtt_state.connection, client->mqtt_state.pending_msg_id);
1561+
#endif
1562+
} else {
1563+
mqtt_msg_pubrel(&client->mqtt_state.connection, client->mqtt_state.pending_msg_id);
1564+
}
1565+
if (client->mqtt_state.connection.outbound_message.length == 0) {
1566+
ESP_LOGE(TAG, "Publish response message PUBREL cannot be created");
1567+
return ESP_FAIL;
1568+
}
1569+
1570+
if (esp_mqtt_write(client) != ESP_OK) {
1571+
ESP_LOGE(TAG, "Error to resend data ");
1572+
esp_mqtt_abort_connection(client);
1573+
return ESP_FAIL;
1574+
}
1575+
1576+
return ESP_OK;
1577+
}
1578+
15531579
static void mqtt_delete_expired_messages(esp_mqtt_client_handle_t client)
15541580
{
15551581
// Delete message after OUTBOX_EXPIRED_TIMEOUT_MS milliseconds
@@ -1723,6 +1749,16 @@ static void esp_mqtt_task(void *pv)
17231749
if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) {
17241750
esp_mqtt5_increment_packet_counter(client);
17251751
}
1752+
#endif
1753+
}
1754+
}
1755+
item = outbox_dequeue(client->outbox, ACKNOWLEDGED, &msg_tick);
1756+
if (item && (last_retransmit - msg_tick > client->config->message_retransmit_timeout)) {
1757+
if (mqtt_resend_pubrel(client, item) == ESP_OK) {
1758+
#ifdef MQTT_PROTOCOL_5
1759+
if (client->mqtt_state.connection.information.protocol_ver == MQTT_PROTOCOL_V_5) {
1760+
esp_mqtt5_increment_packet_counter(client);
1761+
}
17261762
#endif
17271763
}
17281764
}

0 commit comments

Comments
 (0)