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 <flavio.santes@intel.com>
This commit is contained in:
Flavio Santes 2017-01-14 11:08:11 -06:00 committed by Jukka Rissanen
commit 9e6a6f3bce
2 changed files with 19 additions and 2 deletions

View file

@ -139,6 +139,7 @@ struct mqtt_ctx {
* MQTT_APP_PUBLISHER_SUBSCRIBER receives the MQTT UNSUBACK message
*
* <b>Note: this callback must be not NULL</b>
*
* @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 *);

View file

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