I ran in an issue regarding the calculation of the paket length and could not find an open ticket to that so I opened a new one. Please correct me if I'm mistaken or overlooked something, but I think the calculation of the message length at the MQTT-SN implementation for embedded systems is currently wrong.
int MQTTSNSerialize_publishLength(int payloadlen, MQTTSN_topicid topic, int qos)
{
int len = 6;
if (topic.type == MQTTSN_TOPIC_TYPE_NORMAL && qos == 3)
len += topic.data.long_.len;
return payloadlen + len;
}
int MQTTSNPacket_encode(unsigned char* buf, int length)
{
int rc = 0;
FUNC_ENTRY;
if (length > 255)
{
writeChar(&buf, 0x01);
writeInt(&buf, length);
rc += 3;
}
else
buf[rc++] = length;
FUNC_EXIT_RC(rc);
return rc;
}
The length of 256 Byte is given to the function MQTTSNPacket_encode which inserts three bytes of length information (two more than decided in function MQTTSNPacket_len) at the start of the message. After finishing MQTTSNSerialize_publish we have a Message that has the length of 258 Byte, but the length information states that the message length is only 256 Byte.
Therefore I think the current implementation of MQTTSNPacket_len is incorrect.
In my oppinion the correct implementation for MQTTSNPacket_len() needs to add one byte to the length first and evaluate whether there needs to be added two further length bytes:
int MQTTSNPacket_len(int length)
{
int len = length + 1;
if (len > 255)
len += 2
return len;
}
Hi,
I ran in an issue regarding the calculation of the paket length and could not find an open ticket to that so I opened a new one. Please correct me if I'm mistaken or overlooked something, but I think the calculation of the message length at the MQTT-SN implementation for embedded systems is currently wrong.
The current implementation (https://github.com/eclipse/paho.mqtt-sn.embedded-c/blob/master/MQTTSNPacket/src/MQTTSNPacket.c) states:
This function does not consider the field "length" itself in the condition. This leads to a problem, when exactly hitting the message length of 255 Byte (payload plus header without the length information), e. g. constructing a publish containing 249 Bytes of payload and using the current implementation (https://github.com/eclipse/paho.mqtt-sn.embedded-c/blob/master/MQTTSNPacket/src/MQTTSNSerializePublish.c) :
The function MQTTSNSerialize_publishLength (https://github.com/eclipse/paho.mqtt-sn.embedded-c/blob/master/MQTTSNPacket/src/MQTTSNSerializePublish.c) adds 6 Byte to the total length message (here QoS < 3 to simplify the problem):
Therefore MQTTSNPacket_len is called with the parameter length equal to 255. The function returns the length of 256 Byte only considering one Byte for the message length. Now the function MQTTSNPacket_encode (https://github.com/eclipse/paho.mqtt-sn.embedded-c/blob/master/MQTTSNPacket/src/MQTTSNPacket.c) is called:
The length of 256 Byte is given to the function MQTTSNPacket_encode which inserts three bytes of length information (two more than decided in function MQTTSNPacket_len) at the start of the message. After finishing MQTTSNSerialize_publish we have a Message that has the length of 258 Byte, but the length information states that the message length is only 256 Byte.
Therefore I think the current implementation of MQTTSNPacket_len is incorrect.
In my oppinion the correct implementation for MQTTSNPacket_len() needs to add one byte to the length first and evaluate whether there needs to be added two further length bytes:
Alternetively it can correct the condition to
or
or
Please let me know soon if you can confirm my assumption or where I made a mistake.
best regards Johannes