From 9e6a6f3bcee4b4ffd2e24cc9558ed7263db684a9 Mon Sep 17 00:00:00 2001 From: Flavio Santes Date: Sat, 14 Jan 2017 11:08:11 -0600 Subject: [PATCH] net/mqtt: Add the "malformed" callback to the MQTT ctx structure Add the malformed callback that will be executed when a message is received and it does not follow the MQTT v3.1.1 spec. There is another case when this callback may be executed: when the IP stack reception buffer's size is not enough to hold an MQTT message. The publisher and subscriber parser routines are updated to make use of this callback. Inline documentation is also updated. Change-Id: Id1d34336c4322673ca85f2db0b8d432db3c9afa8 Signed-off-by: Flavio Santes --- include/net/mqtt.h | 11 +++++++++++ subsys/net/lib/mqtt/mqtt.c | 10 ++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/include/net/mqtt.h b/include/net/mqtt.h index 9ea6fbfeadd..e432d3c576a 100644 --- a/include/net/mqtt.h +++ b/include/net/mqtt.h @@ -139,6 +139,7 @@ struct mqtt_ctx { * MQTT_APP_PUBLISHER_SUBSCRIBER receives the MQTT UNSUBACK message * * Note: this callback must be not NULL + * * @param [in] ctx MQTT context * @param [in] pkt_id Packet Identifier for the MQTT SUBACK msg * @return If this callback returns 0, the caller will @@ -148,6 +149,16 @@ struct mqtt_ctx { */ int (*unsubscribe)(struct mqtt_ctx *ctx, uint16_t pkt_id); + /** Callback executed when an incoming message doesn't pass the + * validation stage. This callback may be NULL. + * The pkt_type variable may be set to MQTT_INVALID, if the parsing + * stage is aborted before determining the MQTT msg packet type. + * + * @param [in] ctx MQTT context + * @param [in] pkt_type MQTT Packet type + */ + void (*malformed)(struct mqtt_ctx *ctx, uint16_t pkt_type); + /* Internal use only */ int (*rcv)(struct mqtt_ctx *ctx, struct net_buf *); diff --git a/subsys/net/lib/mqtt/mqtt.c b/subsys/net/lib/mqtt/mqtt.c index 9b947931dd6..d3c58630263 100644 --- a/subsys/net/lib/mqtt/mqtt.c +++ b/subsys/net/lib/mqtt/mqtt.c @@ -676,6 +676,8 @@ exit_error: /** * @brief mqtt_publisher_parser Calls the appropriate rx routine for the MQTT * message contained in rx + * @details On error, this routine will execute the + * 'ctx->malformed' callback (if defined) * @param ctx MQTT context * @param rx RX buffer * @return 0 on success @@ -726,7 +728,9 @@ int mqtt_publisher_parser(struct mqtt_ctx *ctx, struct net_buf *rx) } exit_parser: - /* TODO: add error handling via a user provided callback */ + if (rc != 0 && ctx->malformed) { + ctx->malformed(ctx, pkt_type); + } net_nbuf_unref(data); @@ -790,7 +794,9 @@ int mqtt_subscriber_parser(struct mqtt_ctx *ctx, struct net_buf *rx) } exit_parser: - /* TODO: add error handling via a user provided callback */ + if (rc != 0 && ctx->malformed) { + ctx->malformed(ctx, pkt_type); + } net_nbuf_unref(data);