emcute: fix length field calculation#11958
Conversation
The length field in an MQTT packet carries the _total_ length of the packet. If it is below 256 (i.e. fits in one byte) only one byte is used for the length field. If it is larger than that 3 bytes are used, with the first byte having the value `0x01` and the remaining bytes representing the length in as a 2 byte unsigned integer in network byte order. Resulting from that it can be assessed that the check in `emcutes`'s `set_len()` function is wrong as it needs to be checked if `len` is lesser or equal to `0xff - 1`. `len <= (0xff - 1)` can be simplified to `len < 0xff`. For some larger packages this safes 2 bytes of wasted packet space.
|
The same logic is used in the
Apparently MQTT-SN != MQTT. So yes, the code is correct. |
benpicco
left a comment
There was a problem hiding this comment.
Confirmed by reading the documentation.
That explains, why I was confused by your mail. Let's give Murdock another spin at this before merging, just in case. |
|
Ahhh sorry, did not see that you triggered Murdock another hour ago. I thought the last run was ages past. |
The length field in an MQTT packet carries the total length of the packet. If it is below 256 (i.e. fits in one byte) only one byte is used for the length field. If it is larger than that 3 bytes are used, with the first byte having the value
0x01and the remaining bytes representing the length in as a 2 byte unsigned integer in network byte order. Resulting from that it can be assessed that the check inemcutes'sset_len()function is wrong as it needs to be checked iflenis lesser or equal to0xff - 1(-1 for the length field itself).len <= (0xff - 1)can be simplified tolen < 0xff. For some larger packages this safes 2 bytes of wasted packet space.Contribution description
Testing procedure
Merge #11823 and run its testing procedure.
Issues/PRs references
Split out of #11823 to stream-line the review.