2016-01-02 07:41:28 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2014 IBM Corp.
|
|
|
|
*
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
|
|
|
*
|
|
|
|
* The Eclipse Public License is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
* and the Eclipse Distribution License is available at
|
|
|
|
* http://www.eclipse.org/org/documents/edl-v10.php.
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* Ian Craggs - initial API and implementation and/or initial documentation
|
|
|
|
*******************************************************************************/
|
|
|
|
#include <espressif/esp_common.h>
|
|
|
|
#include "StackTrace.h"
|
|
|
|
#include "MQTTPacket.h"
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define min(a, b) ((a < b) ? 1 : 0)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserializes the supplied (wire) buffer into publish data
|
|
|
|
* @param dup returned integer - the MQTT dup flag
|
|
|
|
* @param qos returned integer - the MQTT QoS value
|
|
|
|
* @param retained returned integer - the MQTT retained flag
|
|
|
|
* @param packetid returned integer - the MQTT packet identifier
|
|
|
|
* @param topicName returned MQTTString - the MQTT topic in the publish
|
|
|
|
* @param payload returned byte buffer - the MQTT publish payload
|
|
|
|
* @param payloadlen returned integer - the length of the MQTT payload
|
|
|
|
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
|
|
|
* @param buflen the length in bytes of the data in the supplied buffer
|
|
|
|
* @return error code. 1 is success
|
|
|
|
*/
|
2016-09-15 17:52:57 +00:00
|
|
|
int mqtt_deserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, mqtt_string_t* topicName,
|
2016-01-02 07:41:28 +00:00
|
|
|
unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen)
|
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_header_t header = {0};
|
2016-01-02 07:41:28 +00:00
|
|
|
unsigned char* curdata = buf;
|
|
|
|
unsigned char* enddata = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
int mylen = 0;
|
|
|
|
|
|
|
|
FUNC_ENTRY;
|
2016-09-15 17:52:57 +00:00
|
|
|
header.byte = mqtt_read_char(&curdata);
|
|
|
|
if (header.bits.type != MQTTPACKET_PUBLISH)
|
2016-01-02 07:41:28 +00:00
|
|
|
goto exit;
|
|
|
|
*dup = header.bits.dup;
|
|
|
|
*qos = header.bits.qos;
|
|
|
|
*retained = header.bits.retain;
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
curdata += (rc = mqtt_packet_decode_buf(curdata, &mylen)); /* read remaining length */
|
2016-01-02 07:41:28 +00:00
|
|
|
enddata = curdata + mylen;
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
if (!mqtt_read_str_len(topicName, &curdata, enddata) ||
|
2016-01-02 07:41:28 +00:00
|
|
|
enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
if (*qos > 0)
|
2016-09-15 17:52:57 +00:00
|
|
|
*packetid = mqtt_read_int(&curdata);
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
*payloadlen = enddata - curdata;
|
|
|
|
*payload = curdata;
|
|
|
|
rc = 1;
|
|
|
|
exit:
|
|
|
|
FUNC_EXIT_RC(rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deserializes the supplied (wire) buffer into an ack
|
|
|
|
* @param packettype returned integer - the MQTT packet type
|
|
|
|
* @param dup returned integer - the MQTT dup flag
|
|
|
|
* @param packetid returned integer - the MQTT packet identifier
|
|
|
|
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
|
|
|
* @param buflen the length in bytes of the data in the supplied buffer
|
|
|
|
* @return error code. 1 is success, 0 is failure
|
|
|
|
*/
|
2016-09-15 17:52:57 +00:00
|
|
|
int mqtt_deserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_header_t header = {0};
|
2016-01-02 07:41:28 +00:00
|
|
|
unsigned char* curdata = buf;
|
|
|
|
unsigned char* enddata = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
int mylen;
|
|
|
|
|
|
|
|
FUNC_ENTRY;
|
2016-09-15 17:52:57 +00:00
|
|
|
header.byte = mqtt_read_char(&curdata);
|
2016-01-02 07:41:28 +00:00
|
|
|
*dup = header.bits.dup;
|
|
|
|
*packettype = header.bits.type;
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
curdata += (rc = mqtt_packet_decode_buf(curdata, &mylen)); /* read remaining length */
|
2016-01-02 07:41:28 +00:00
|
|
|
enddata = curdata + mylen;
|
|
|
|
|
|
|
|
if (enddata - curdata < 2)
|
|
|
|
goto exit;
|
2016-09-15 17:52:57 +00:00
|
|
|
*packetid = mqtt_read_int(&curdata);
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
rc = 1;
|
|
|
|
exit:
|
|
|
|
FUNC_EXIT_RC(rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|