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);