Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

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