From 23d9065f2b4c95e6b3a8b707fbd64c3f39529447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20G=C3=A4hwiler?= Date: Wed, 18 Oct 2017 16:27:49 +0300 Subject: [PATCH] be more careful with qos levels --- src/packet.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/packet.c b/src/packet.c index 2a35c32..1ac57f8 100644 --- a/src/packet.c +++ b/src/packet.c @@ -415,11 +415,28 @@ lwmqtt_err_t lwmqtt_decode_publish(uint8_t *buf, size_t buf_len, bool *dup, uint return LWMQTT_MISSING_OR_WRONG_PACKET; } - // set variables + // set dup flag *dup = header.bits.dup == 1; - msg->qos = (lwmqtt_qos_t)header.bits.qos; + + // set retained flag msg->retained = header.bits.retain == 1; + // set qos + switch (header.bits.qos) { + case 0: + msg->qos = LWMQTT_QOS0; + break; + case 1: + msg->qos = LWMQTT_QOS1; + break; + case 2: + msg->qos = LWMQTT_QOS2; + break; + default: + msg->qos = LWMQTT_QOS0; + break; + } + // read remaining length uint32_t rem_len; err = lwmqtt_read_varnum(&buf_ptr, buf_end, &rem_len); @@ -643,7 +660,20 @@ lwmqtt_err_t lwmqtt_decode_suback(uint8_t *buf, size_t buf_len, uint16_t *packet } // set qos level - granted_qos_levels[*count] = (lwmqtt_qos_t)raw_qos_level; + switch (raw_qos_level) { + case 0: + granted_qos_levels[*count] = LWMQTT_QOS0; + break; + case 1: + granted_qos_levels[*count] = LWMQTT_QOS1; + break; + case 2: + granted_qos_levels[*count] = LWMQTT_QOS2; + break; + default: + granted_qos_levels[*count] = LWMQTT_QOS_FAILURE; + break; + } } return LWMQTT_SUCCESS;