Version to make it easier to reuse without source modifications

Committer:
JMF
Date:
Tue Mar 27 17:26:35 2018 +0000
Revision:
0:5cd4781e0c88
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:5cd4781e0c88 1 /*******************************************************************************
JMF 0:5cd4781e0c88 2 * Copyright (c) 2014 IBM Corp.
JMF 0:5cd4781e0c88 3 *
JMF 0:5cd4781e0c88 4 * All rights reserved. This program and the accompanying materials
JMF 0:5cd4781e0c88 5 * are made available under the terms of the Eclipse Public License v1.0
JMF 0:5cd4781e0c88 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
JMF 0:5cd4781e0c88 7 *
JMF 0:5cd4781e0c88 8 * The Eclipse Public License is available at
JMF 0:5cd4781e0c88 9 * http://www.eclipse.org/legal/epl-v10.html
JMF 0:5cd4781e0c88 10 * and the Eclipse Distribution License is available at
JMF 0:5cd4781e0c88 11 * http://www.eclipse.org/org/documents/edl-v10.php.
JMF 0:5cd4781e0c88 12 *
JMF 0:5cd4781e0c88 13 * Contributors:
JMF 0:5cd4781e0c88 14 * Ian Craggs - initial API and implementation and/or initial documentation
JMF 0:5cd4781e0c88 15 *******************************************************************************/
JMF 0:5cd4781e0c88 16
JMF 0:5cd4781e0c88 17 #include "StackTrace.h"
JMF 0:5cd4781e0c88 18 #include "MQTTPacket.h"
JMF 0:5cd4781e0c88 19 #include <string.h>
JMF 0:5cd4781e0c88 20
JMF 0:5cd4781e0c88 21 #define min(a, b) ((a < b) ? 1 : 0)
JMF 0:5cd4781e0c88 22
JMF 0:5cd4781e0c88 23 /**
JMF 0:5cd4781e0c88 24 * Deserializes the supplied (wire) buffer into publish data
JMF 0:5cd4781e0c88 25 * @param dup returned integer - the MQTT dup flag
JMF 0:5cd4781e0c88 26 * @param qos returned integer - the MQTT QoS value
JMF 0:5cd4781e0c88 27 * @param retained returned integer - the MQTT retained flag
JMF 0:5cd4781e0c88 28 * @param packetid returned integer - the MQTT packet identifier
JMF 0:5cd4781e0c88 29 * @param topicName returned MQTTString - the MQTT topic in the publish
JMF 0:5cd4781e0c88 30 * @param payload returned byte buffer - the MQTT publish payload
JMF 0:5cd4781e0c88 31 * @param payloadlen returned integer - the length of the MQTT payload
JMF 0:5cd4781e0c88 32 * @param buf the raw buffer data, of the correct length determined by the remaining length field
JMF 0:5cd4781e0c88 33 * @param buflen the length in bytes of the data in the supplied buffer
JMF 0:5cd4781e0c88 34 * @return error code. 1 is success
JMF 0:5cd4781e0c88 35 */
JMF 0:5cd4781e0c88 36 int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
JMF 0:5cd4781e0c88 37 unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen)
JMF 0:5cd4781e0c88 38 {
JMF 0:5cd4781e0c88 39 MQTTHeader header = {0};
JMF 0:5cd4781e0c88 40 unsigned char* curdata = buf;
JMF 0:5cd4781e0c88 41 unsigned char* enddata = NULL;
JMF 0:5cd4781e0c88 42 int rc = 0;
JMF 0:5cd4781e0c88 43 int mylen = 0;
JMF 0:5cd4781e0c88 44
JMF 0:5cd4781e0c88 45 FUNC_ENTRY;
JMF 0:5cd4781e0c88 46 header.byte = readChar(&curdata);
JMF 0:5cd4781e0c88 47 if (header.bits.type != PUBLISH)
JMF 0:5cd4781e0c88 48 goto exit;
JMF 0:5cd4781e0c88 49 *dup = header.bits.dup;
JMF 0:5cd4781e0c88 50 *qos = header.bits.qos;
JMF 0:5cd4781e0c88 51 *retained = header.bits.retain;
JMF 0:5cd4781e0c88 52
JMF 0:5cd4781e0c88 53 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
JMF 0:5cd4781e0c88 54 enddata = curdata + mylen;
JMF 0:5cd4781e0c88 55
JMF 0:5cd4781e0c88 56 if (!readMQTTLenString(topicName, &curdata, enddata) ||
JMF 0:5cd4781e0c88 57 enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
JMF 0:5cd4781e0c88 58 goto exit;
JMF 0:5cd4781e0c88 59
JMF 0:5cd4781e0c88 60 if (*qos > 0)
JMF 0:5cd4781e0c88 61 *packetid = readInt(&curdata);
JMF 0:5cd4781e0c88 62
JMF 0:5cd4781e0c88 63 *payloadlen = enddata - curdata;
JMF 0:5cd4781e0c88 64 *payload = curdata;
JMF 0:5cd4781e0c88 65 rc = 1;
JMF 0:5cd4781e0c88 66 exit:
JMF 0:5cd4781e0c88 67 FUNC_EXIT_RC(rc);
JMF 0:5cd4781e0c88 68 return rc;
JMF 0:5cd4781e0c88 69 }
JMF 0:5cd4781e0c88 70
JMF 0:5cd4781e0c88 71
JMF 0:5cd4781e0c88 72
JMF 0:5cd4781e0c88 73 /**
JMF 0:5cd4781e0c88 74 * Deserializes the supplied (wire) buffer into an ack
JMF 0:5cd4781e0c88 75 * @param packettype returned integer - the MQTT packet type
JMF 0:5cd4781e0c88 76 * @param dup returned integer - the MQTT dup flag
JMF 0:5cd4781e0c88 77 * @param packetid returned integer - the MQTT packet identifier
JMF 0:5cd4781e0c88 78 * @param buf the raw buffer data, of the correct length determined by the remaining length field
JMF 0:5cd4781e0c88 79 * @param buflen the length in bytes of the data in the supplied buffer
JMF 0:5cd4781e0c88 80 * @return error code. 1 is success, 0 is failure
JMF 0:5cd4781e0c88 81 */
JMF 0:5cd4781e0c88 82 int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen)
JMF 0:5cd4781e0c88 83 {
JMF 0:5cd4781e0c88 84 MQTTHeader header = {0};
JMF 0:5cd4781e0c88 85 unsigned char* curdata = buf;
JMF 0:5cd4781e0c88 86 unsigned char* enddata = NULL;
JMF 0:5cd4781e0c88 87 int rc = 0;
JMF 0:5cd4781e0c88 88 int mylen;
JMF 0:5cd4781e0c88 89
JMF 0:5cd4781e0c88 90 FUNC_ENTRY;
JMF 0:5cd4781e0c88 91 header.byte = readChar(&curdata);
JMF 0:5cd4781e0c88 92 *dup = header.bits.dup;
JMF 0:5cd4781e0c88 93 *packettype = header.bits.type;
JMF 0:5cd4781e0c88 94
JMF 0:5cd4781e0c88 95 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
JMF 0:5cd4781e0c88 96 enddata = curdata + mylen;
JMF 0:5cd4781e0c88 97
JMF 0:5cd4781e0c88 98 if (enddata - curdata < 2)
JMF 0:5cd4781e0c88 99 goto exit;
JMF 0:5cd4781e0c88 100 *packetid = readInt(&curdata);
JMF 0:5cd4781e0c88 101
JMF 0:5cd4781e0c88 102 rc = 1;
JMF 0:5cd4781e0c88 103 exit:
JMF 0:5cd4781e0c88 104 FUNC_EXIT_RC(rc);
JMF 0:5cd4781e0c88 105 return rc;
JMF 0:5cd4781e0c88 106 }
JMF 0:5cd4781e0c88 107