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

MQTT with QOS 1 or 2 forbids a packet ID of 0. #190

Merged
merged 5 commits into from Aug 28, 2023

Conversation

dlizotte-uwo
Copy link
Contributor

  • Describe the scope of your change--i.e. what the change does and what parts
    of the code were modified.
    This will help us understand any risks of integrating
    the code.

The change is to the packet numbering, which now starts at 1 and wraps around to 1 (rather than 0) when overflowing 16 bits. This is because the MQTT standard requires nonzero packet IDs for QOS=1 and QOS=2. In the previous version, if a client is created, then connected, then a QOS=1 subscription packet is sent with packet ID 0 is sent, and my server (mosquitto) disconnects. Re-connecting and trying again works because the packet ID has incremented to 1, which is legal.

  • Describe any known limitations with your change. For example if the change
    doesn't apply to a supported platform of the library please mention it.

I don't think there are any.

  • Please run any tests or examples that can exercise your modified code. We
    strive to not break users of the code and running tests/examples helps with this
    process.

I am using this code in a project and have tried with pub/sub; seems to work now.

@@ -116,7 +116,7 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
will_qos = 0;
will_retain = 0;

packet_id_counter = 0;
packet_id_counter = 1; // MQTT spec forbids packet id of 0 if QOS=1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dlizotte-uwo this constructor does not guarantee a QoS level, we should handle checking and incrementing the PID based on the QoS level within the SUBSCRIBE, UNSUBSCRIBE, and PUBLISH calls.

SUBSCRIBE, UNSUBSCRIBE, and PUBLISH (in cases where QoS > 0) Control Packets MUST contain a non-zero 16-bit Packet Identifier [MQTT-2.3.1-1].

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brentru Hi, the QoS only needs to be checked for PUBLISH (and is already done in L707).
SUBSCRIBE and UNSUBSCRIBE always need a (valid) packet id
(Ref)
In PUBLISH QoS = 0, the packet id is not used.

@@ -137,7 +137,7 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port,
will_qos = 0;
will_retain = 0;

packet_id_counter = 0;
packet_id_counter = 1; // MQTT spec forbids packet id of 0 if QOS=1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

// increment the packet id
packet_id_counter++;
// increment the packet id, skipping 0
packet_id_counter = packet_id_counter + 1 + (packet_id_counter + 1 == 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perform this within the check for QOS<1 on L729 instead

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brentru This is already in a check L707 for QoS > 0 (and for QoS = 0 the counter is not changed and not used)

@brentru
Copy link
Member

brentru commented May 12, 2021

@dlizotte-uwo are you available to make the requested changes above?

@dlizotte-uwo
Copy link
Contributor Author

@dlizotte-uwo are you available to make the requested changes above?

Hi sorry @brentru - day job is madness. I can probably fix this weekend if that's still useful.

@brentru
Copy link
Member

brentru commented May 24, 2021

@dlizotte-uwo It's still useful! I'll keep this issue open for when you're ready.

@dlizotte-uwo dlizotte-uwo requested a review from brentru July 5, 2023 22:14
@dlizotte-uwo
Copy link
Contributor Author

I think based on what fipwmaqzufheoxq92ebc says and based on #199 that this is still needed and checks are in the right place. I think it's good to keep 1 <= packet_id_counter <= 65535 an invariant in this code.

@brentru
Copy link
Member

brentru commented Aug 21, 2023

@tyeth Before I look it over, do you have thoughts on this PR?

@tyeth
Copy link

tyeth commented Aug 22, 2023 via email

@tyeth
Copy link

tyeth commented Aug 24, 2023

@brentru I think we might be okay, I'm fine with where the checks are, and the use of packet id seems like we wont run into conflicts. (Worth rechecking when we support QOS2 or mqtt5)

@tyeth tyeth self-requested a review August 24, 2023 13:14
@brentru
Copy link
Member

brentru commented Aug 28, 2023

Sweet! Thank you for the PR and for your patience, @dlizotte-uwo

@brentru brentru merged commit 3a7cf33 into adafruit:master Aug 28, 2023
1 check passed
@dlizotte-uwo
Copy link
Contributor Author

Thanks @brentru! Sorry for that year I was MIA.

@brentru
Copy link
Member

brentru commented Aug 28, 2023

No problem, thanks for the PR!

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

Successfully merging this pull request may close these issues.

None yet

4 participants