subsys/net/lib/mqtt/mqtt.c
/**
 * Parses and validates the MQTT PUBxxxx message contained in the rx packet.
 *
 *
 * @details It validates against message structure and Packet Identifier.
 * For the MQTT PUBREC and PUBREL messages, this function writes the
 * corresponding MQTT PUB msg.
 *
 * @param ctx MQTT context
 * @param rx RX packet
 * @param type MQTT Packet type
 *
 * @retval 0 on success
 * @retval -EINVAL on error
 */
Parses and validates the MQTT PUBxxxx message contained in the rx packet.
@details It validates against message structure and Packet Identifier.

@param ctx MQTT context
@param rx RX packet
@param type MQTT Packet type

@retval 0 on success
@retval -EINVAL on error
Parses and validates the MQTT PUBxxxx message contained in the rx packet . @details It validates against message structure and Packet Identifier . @param ctx MQTT context @param rx RX packet @param type MQTT Packet type @retval 0 on success @retval - EINVAL on error
static
int mqtt_rx_pub_msgs(struct mqtt_ctx *ctx, struct net_buf *rx,
		     enum mqtt_packet type)
{
	int (*unpack)(u8_t *, u16_t, u16_t *) = NULL;
	int (*response)(struct mqtt_ctx *, u16_t) = NULL;
	u16_t pkt_id;
	u16_t len;
	u8_t *data;
	int rc;
	switch (type) {
	case MQTT_PUBACK:
		unpack = mqtt_unpack_puback;
		break;
	case MQTT_PUBCOMP:
		unpack = mqtt_unpack_pubcomp;
		break;
	case MQTT_PUBREC:
		unpack = mqtt_unpack_pubrec;
		response = mqtt_tx_pubrel;
		break;
	case MQTT_PUBREL:
		unpack = mqtt_unpack_pubrel;
		response = mqtt_tx_pubcomp;
		break;
	default:
		return -EINVAL;
	}
	data = rx->data;
	len = rx->len;
	rc = unpack(data, len, &pkt_id);
	if (rc != 0) {
		return -EINVAL;
	}
	if (type == MQTT_PUBREL) {
		if (ctx->app_type != MQTT_APP_PUBLISHER) {
			rc = ctx->publish_rx(ctx, NULL, pkt_id, MQTT_PUBREL);
		} else {
			rc = -EINVAL;
		}
	} else {
		rc = ctx->publish_tx(ctx, pkt_id, type);
	}
	if (rc != 0) {
		return -EINVAL;
	}
	if (!response)  {
		return 0;
	}
	rc = response(ctx, pkt_id);
	if (rc != 0) {
		return -EINVAL;
	}
	return 0;
}
static int mqtt_rx_pub_msgs ( struct mqtt_ctx * ctx , struct net_buf * rx , enum mqtt_packet type ) { int ( * unpack ) ( u8_t * , u16_t , u16_t * ) = NULL ; int ( * response ) ( struct mqtt_ctx * , u16_t ) = NULL ; u16_t pkt_id ; u16_t len ; u8_t * data ; int rc ; switch ( type ) { case MQTT_PUBACK : unpack = mqtt_unpack_puback ; break ; case MQTT_PUBCOMP : unpack = mqtt_unpack_pubcomp ; break ; case MQTT_PUBREC : unpack = mqtt_unpack_pubrec ; response = mqtt_tx_pubrel ; break ; case MQTT_PUBREL : unpack = mqtt_unpack_pubrel ; response = mqtt_tx_pubcomp ; break ; default : return - EINVAL ; } data = rx -> data ; len = rx -> len ; rc = unpack ( data , len , & pkt_id ) ; if ( rc != 0 ) { return - EINVAL ; } if ( type == MQTT_PUBREL ) { if ( ctx -> app_type != MQTT_APP_PUBLISHER ) { rc = ctx -> publish_rx ( ctx , NULL , pkt_id , MQTT_PUBREL ) ; } else { rc = - EINVAL ; } } else { rc = ctx -> publish_tx ( ctx , pkt_id , type ) ; } if ( rc != 0 ) { return - EINVAL ; } if ( ! response ) { return 0 ; } rc = response ( ctx , pkt_id ) ; if ( rc != 0 ) { return - EINVAL ; } return 0 ; }
Parses and validates the MQTT PUBxxxx message contained in the rx packet.
Parses and validates the MQTT PUBxxxx message contained in the rx packet .
/* 4 bytes message */ /* Only MQTT_APP_SUBSCRIBER, MQTT_APP_PUBLISHER_SUBSCRIBER and
	 * MQTT_APP_SERVER apps must receive the MQTT_PUBREL msg.
	 */