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 <flavio.santes@intel.com>
This commit is contained in:
Flavio Santes 2016-12-15 23:17:12 -06:00 committed by Tomasz Bursztyka
commit d490ff27ad
2 changed files with 44 additions and 0 deletions

View file

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

View file

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