Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the parsing of DISCONNECT packet for MQTT 5 (IDFGH-14489) #295

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

falvia
Copy link

@falvia falvia commented Jan 22, 2025

Description

In the MQTT 5 protocol the broker can disconnect the client with a DISCONNECT packet. This packet contains the reason code of the forced disconnection.
Before these patches the DISCONNECT packet was not parsed and it was not possible to know and check the disconnection reason by the broker at application level.

So now in mqtt_process_receive also the disconnect packets are processed, setting the disconnect_return_code variable in the error_handle structure (as per the connect_return_code) and sending the event to the application event loop.

Related

Testing

I tested it with a mqtt 5 server and 2 ESP32-S3-WROOM-1-N16R8V modules.
When the two modules were connected to the broker with same client ID, the first one was kicked out (as per specification). I wanted to have the reason code of the DISCONNECT packet so if it was 0x8E (MQTT5_SESSION_TAKEN_OVER), the first client is not continuing reconnecting in my application (it was creating a loop of connection and disconnection between the first and the second client).
I found this solutions, but I am open to suggestion! Thanks!


Checklist

Before submitting a Pull Request, please ensure the following:

  • 🚨 This PR does not introduce breaking changes.
  • All CI checks (GH Actions) pass.
  • Documentation is updated as needed.
  • Tests are updated or added as necessary.
  • Code is well-commented, especially in complex areas.
  • Git history is clean — commits are squashed to the minimum necessary.

In mqtt5 protocol the disconnect packet is sent from the broker
with a reason code. Create the function to parse it and get it
back in the disconnect_rsp_code variable.

Signed-off-by: Flavia Caforio <[email protected]>
Add this variable to the esp_mqtt_error_codes_t structure.
In the mqtt5 protocol the broker can disconnect the client with
a disconnect packet. This packet contains a reason value that can be
useful for certain applications in which it is important to know
the reason of disconnection.

Signed-off-by: Flavia Caforio <[email protected]>
While the client is connected is possible that a disconnect packet
is reaceived by the broker to force a disconnection. Before this
patch this approach causes a generic error on transport in case of
disconnection from the broker. If the packet is managed before getting
an error it is possible to save the reason code in the
disconnect_return_code variable in the error_handle, and dispatch
the disconnect event that can be managed by the application event
loop, that now can know the reason of disconnection from the broker.
Reset the variable in case of error.

Signed-off-by: Flavia Caforio <[email protected]>
@github-actions github-actions bot changed the title Add the parsing of DISCONNECT packet for MQTT 5 Add the parsing of DISCONNECT packet for MQTT 5 (IDFGH-14489) Jan 22, 2025
@falvia
Copy link
Author

falvia commented Feb 11, 2025

Hi @bogdankolendovskyy! Sorry to bother you, can you please approve the workflows?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants