From d490ff27adbbee741ebd9616cb04028ecd069a89 Mon Sep 17 00:00:00 2001 From: Flavio Santes Date: Thu, 15 Dec 2016 23:17:12 -0600 Subject: [PATCH] net/mqtt: Add the mqtt_rx_publish routine The mqtt_rx_publish is used to process incoming MQTT PUBLISH messages. This routine performes the following steps: - take ownership of the rx buffer containing the MQTT PUBLISH msg, - call the MQTT parser routine, and - determine, based on the incoming message's MQTT QoS, the next action. Change-Id: I3cc011cf0c280205161d0484f12a2cfa79fdf44a Signed-off-by: Flavio Santes --- include/net/mqtt.h | 10 ++++++++++ subsys/net/lib/mqtt/mqtt.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/include/net/mqtt.h b/include/net/mqtt.h index 14214e61016..5b682bd62f6 100644 --- a/include/net/mqtt.h +++ b/include/net/mqtt.h @@ -403,4 +403,14 @@ int mqtt_rx_suback(struct mqtt_ctx *ctx, struct net_buf *rx); */ int mqtt_rx_unsuback(struct mqtt_ctx *ctx, struct net_buf *rx); +/** + * @brief mqtt_rx_publish Parses the MQTT PUBLISH message + * @param [in] ctx MQTT context structure + * @param [in] rx RX buffer from the IP stack + * @return 0 on success + * @return -EINVAL on error + * @return -ENOMEM if no data buffer is available + */ +int mqtt_rx_publish(struct mqtt_ctx *ctx, struct net_buf *rx); + #endif diff --git a/subsys/net/lib/mqtt/mqtt.c b/subsys/net/lib/mqtt/mqtt.c index d88011268c2..f8d868807fb 100644 --- a/subsys/net/lib/mqtt/mqtt.c +++ b/subsys/net/lib/mqtt/mqtt.c @@ -595,3 +595,37 @@ int mqtt_rx_unsuback(struct mqtt_ctx *ctx, struct net_buf *rx) return 0; } + +int mqtt_rx_publish(struct mqtt_ctx *ctx, struct net_buf *rx) +{ + struct mqtt_publish_msg msg; + int rc; + + rc = mqtt_unpack_publish(rx->data, rx->len, &msg); + if (rc != 0) { + return -EINVAL; + } + + rc = ctx->publish_rx(ctx->publish_rx_data, &msg, msg.pkt_id, + MQTT_PUBLISH); + if (rc != 0) { + return -EINVAL; + } + + switch (msg.qos) { + case MQTT_QoS2: + rc = mqtt_tx_pubrec(ctx, msg.pkt_id); + break; + case MQTT_QoS1: + rc = mqtt_tx_puback(ctx, msg.pkt_id); + break; + case MQTT_QoS0: + break; + default: + rc = -EINVAL; + break; + } + + return rc; +} +