Skip to content

Commit 6427aca

Browse files
committed
[nrf fromtree] net: mqtt: Add MQTT 5.0 support for SUBACK/UNSUBACK
Add support for SUBACK/UNSUBACK messaged specified in MQTT 5.0. Signed-off-by: Robert Lubos <[email protected]> (cherry picked from commit 2459ffa)
1 parent ef64e18 commit 6427aca

File tree

5 files changed

+91
-12
lines changed

5 files changed

+91
-12
lines changed

include/zephyr/net/mqtt.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,16 +411,32 @@ struct mqtt_pubcomp_param {
411411
struct mqtt_suback_param {
412412
/** Message id of the SUBSCRIBE message being acknowledged */
413413
uint16_t message_id;
414-
/** Return codes indicating maximum QoS level granted for each topic
415-
* in the subscription list.
414+
415+
/** MQTT 3.1 - Return codes indicating maximum QoS level granted for
416+
* each topic in the subscription list.
417+
* MQTT 5.0 - Reason codes corresponding to each topic in the
418+
* subscription list.
416419
*/
417420
struct mqtt_binstr return_codes;
421+
422+
#if defined(CONFIG_MQTT_VERSION_5_0)
423+
/** MQTT 5.0 properties. */
424+
struct mqtt_common_ack_properties prop;
425+
#endif /* CONFIG_MQTT_VERSION_5_0 */
418426
};
419427

420428
/** @brief Parameters for MQTT unsubscribe acknowledgment (UNSUBACK). */
421429
struct mqtt_unsuback_param {
422430
/** Message id of the UNSUBSCRIBE message being acknowledged */
423431
uint16_t message_id;
432+
433+
#if defined(CONFIG_MQTT_VERSION_5_0)
434+
/** Reason codes corresponding to each topic in the unsubscription list. */
435+
struct mqtt_binstr reason_codes;
436+
437+
/** MQTT 5.0 properties. */
438+
struct mqtt_common_ack_properties prop;
439+
#endif /* CONFIG_MQTT_VERSION_5_0 */
424440
};
425441

426442
/** @brief Parameters for a publish message (PUBLISH). */

subsys/net/lib/mqtt/mqtt_decoder.c

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -935,10 +935,28 @@ int publish_complete_decode(const struct mqtt_client *client, struct buf_ctx *bu
935935
}
936936
#endif
937937

938-
return common_ack_decode(buf, &param->message_id, reason_code, prop);
938+
return common_pub_ack_decode(buf, &param->message_id, reason_code, prop);
939939
}
940940

941-
int subscribe_ack_decode(struct buf_ctx *buf, struct mqtt_suback_param *param)
941+
#if defined(CONFIG_MQTT_VERSION_5_0)
942+
static int suback_properties_decode(struct buf_ctx *buf,
943+
struct mqtt_suback_param *param)
944+
{
945+
return common_ack_properties_decode(buf, &param->prop);
946+
}
947+
#else
948+
static int suback_properties_decode(struct buf_ctx *buf,
949+
struct mqtt_suback_param *param)
950+
{
951+
ARG_UNUSED(param);
952+
ARG_UNUSED(buf);
953+
954+
return -ENOTSUP;
955+
}
956+
#endif /* CONFIG_MQTT_VERSION_5_0 */
957+
958+
int subscribe_ack_decode(const struct mqtt_client *client, struct buf_ctx *buf,
959+
struct mqtt_suback_param *param)
942960
{
943961
int err_code;
944962

@@ -947,11 +965,53 @@ int subscribe_ack_decode(struct buf_ctx *buf, struct mqtt_suback_param *param)
947965
return err_code;
948966
}
949967

968+
if (mqtt_is_version_5_0(client)) {
969+
err_code = suback_properties_decode(buf, param);
970+
if (err_code < 0) {
971+
return err_code;
972+
}
973+
}
974+
950975
return unpack_raw_data(buf->end - buf->cur, buf, &param->return_codes);
951976
}
952977

953-
int unsubscribe_ack_decode(struct buf_ctx *buf,
978+
#if defined(CONFIG_MQTT_VERSION_5_0)
979+
static int unsuback_5_0_decode(struct buf_ctx *buf,
980+
struct mqtt_unsuback_param *param)
981+
{
982+
int err;
983+
984+
err = common_ack_properties_decode(buf, &param->prop);
985+
if (err < 0) {
986+
return err;
987+
}
988+
989+
return unpack_raw_data(buf->end - buf->cur, buf, &param->reason_codes);
990+
}
991+
#else
992+
static int unsuback_5_0_decode(struct buf_ctx *buf,
993+
struct mqtt_unsuback_param *param)
994+
{
995+
ARG_UNUSED(param);
996+
ARG_UNUSED(buf);
997+
998+
return -ENOTSUP;
999+
}
1000+
#endif /* CONFIG_MQTT_VERSION_5_0 */
1001+
1002+
int unsubscribe_ack_decode(const struct mqtt_client *client, struct buf_ctx *buf,
9541003
struct mqtt_unsuback_param *param)
9551004
{
956-
return unpack_uint16(buf, &param->message_id);
1005+
int err;
1006+
1007+
err = unpack_uint16(buf, &param->message_id);
1008+
if (err < 0) {
1009+
return 0;
1010+
}
1011+
1012+
if (mqtt_is_version_5_0(client)) {
1013+
return unsuback_5_0_decode(buf, param);
1014+
}
1015+
1016+
return 0;
9571017
}

subsys/net/lib/mqtt/mqtt_internal.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,24 +423,26 @@ int publish_complete_decode(const struct mqtt_client *client, struct buf_ctx *bu
423423

424424
/**@brief Decode MQTT Subscribe packet.
425425
*
426+
* @param[in] MQTT client for which packet is decoded.
426427
* @param[inout] buf A pointer to the buf_ctx structure containing current
427428
* buffer position.
428429
* @param[out] param Pointer to buffer for decoded Subscribe parameters.
429430
*
430431
* @return 0 if the procedure is successful, an error code otherwise.
431432
*/
432-
int subscribe_ack_decode(struct buf_ctx *buf,
433+
int subscribe_ack_decode(const struct mqtt_client *client, struct buf_ctx *buf,
433434
struct mqtt_suback_param *param);
434435

435436
/**@brief Decode MQTT Unsubscribe packet.
436437
*
438+
* @param[in] MQTT client for which packet is decoded.
437439
* @param[inout] buf A pointer to the buf_ctx structure containing current
438440
* buffer position.
439441
* @param[out] param Pointer to buffer for decoded Unsubscribe parameters.
440442
*
441443
* @return 0 if the procedure is successful, an error code otherwise.
442444
*/
443-
int unsubscribe_ack_decode(struct buf_ctx *buf,
445+
int unsubscribe_ack_decode(const struct mqtt_client *client, struct buf_ctx *buf,
444446
struct mqtt_unsuback_param *param);
445447

446448
/**

subsys/net/lib/mqtt/mqtt_rx.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,16 @@ static int mqtt_handle_packet(struct mqtt_client *client,
113113
NET_DBG("[CID %p]: Received MQTT_PKT_TYPE_SUBACK!", client);
114114

115115
evt.type = MQTT_EVT_SUBACK;
116-
err_code = subscribe_ack_decode(buf, &evt.param.suback);
116+
err_code = subscribe_ack_decode(client, buf, &evt.param.suback);
117117
evt.result = err_code;
118118
break;
119119

120120
case MQTT_PKT_TYPE_UNSUBACK:
121121
NET_DBG("[CID %p]: Received MQTT_PKT_TYPE_UNSUBACK!", client);
122122

123123
evt.type = MQTT_EVT_UNSUBACK;
124-
err_code = unsubscribe_ack_decode(buf, &evt.param.unsuback);
124+
err_code = unsubscribe_ack_decode(client, buf,
125+
&evt.param.unsuback);
125126
evt.result = err_code;
126127
break;
127128

tests/net/lib/mqtt/v3_1_1/mqtt_packet/src/mqtt_packet.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ static int eval_msg_suback(struct mqtt_test *mqtt_test)
874874

875875
zassert_false(rc, "fixed_header_decode failed");
876876

877-
rc = subscribe_ack_decode(&buf, &dec_param);
877+
rc = subscribe_ack_decode(&client, &buf, &dec_param);
878878

879879
/**TESTPOINT: Check subscribe_ack_decode function*/
880880
zassert_false(rc, "subscribe_ack_decode failed");
@@ -1083,7 +1083,7 @@ static int eval_msg_unsuback(struct mqtt_test *mqtt_test)
10831083

10841084
zassert_false(rc, "fixed_header_decode failed");
10851085

1086-
rc = unsubscribe_ack_decode(&buf, &dec_param);
1086+
rc = unsubscribe_ack_decode(&client, &buf, &dec_param);
10871087

10881088
zassert_false(rc, "unsubscribe_ack_decode failed");
10891089

0 commit comments

Comments
 (0)