The Cayenne MQTT mbed Library provides functions to easily connect to the Cayenne IoT project builder.

Dependents:   Cayenne-ESP8266Interface Cayenne-WIZnet_Library Cayenne-WIZnetInterface Cayenne-X-NUCLEO-IDW01M1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTDeserializePublish.c Source File

MQTTDeserializePublish.c

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *******************************************************************************/
00016 
00017 #include "MQTTPacket.h"
00018 #include <string.h>
00019 
00020 #define min(a, b) ((a < b) ? 1 : 0)
00021 
00022 /**
00023   * Deserializes the supplied (wire) buffer into publish data
00024   * @param dup returned integer - the MQTT dup flag
00025   * @param qos returned integer - the MQTT QoS value
00026   * @param retained returned integer - the MQTT retained flag
00027   * @param packetid returned integer - the MQTT packet identifier
00028   * @param topicName returned MQTTString - the MQTT topic in the publish
00029   * @param payload returned byte buffer - the MQTT publish payload
00030   * @param payloadlen returned integer - the length of the MQTT payload
00031   * @param buf the raw buffer data, of the correct length determined by the remaining length field
00032   * @param buflen the length in bytes of the data in the supplied buffer
00033   * @return error code.  1 is success
00034   */
00035 int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
00036         unsigned char** payload, size_t* payloadlen, unsigned char* buf, size_t buflen)
00037 {
00038     MQTTHeader header = {0};
00039     unsigned char* curdata = buf;
00040     unsigned char* enddata = NULL;
00041     int rc = 0;
00042     int mylen = 0;
00043 
00044     header.byte = readChar(&curdata);
00045     if (header.bits.type != PUBLISH_MSG)
00046         goto exit;
00047     *dup = header.bits.dup;
00048     *qos = header.bits.qos;
00049     *retained = header.bits.retain;
00050 
00051     curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
00052     enddata = curdata + mylen;
00053 
00054     if (!readMQTTLenString(topicName, &curdata, enddata) ||
00055         enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
00056         goto exit;
00057 
00058     if (*qos > 0)
00059         *packetid = readInt(&curdata);
00060 
00061     *payloadlen = enddata - curdata;
00062     *payload = curdata;
00063     rc = 1;
00064 exit:
00065     return rc;
00066 }
00067 
00068 
00069 
00070 /**
00071   * Deserializes the supplied (wire) buffer into an ack
00072   * @param packettype returned integer - the MQTT packet type
00073   * @param dup returned integer - the MQTT dup flag
00074   * @param packetid returned integer - the MQTT packet identifier
00075   * @param buf the raw buffer data, of the correct length determined by the remaining length field
00076   * @param buflen the length in bytes of the data in the supplied buffer
00077   * @return error code.  1 is success, 0 is failure
00078   */
00079 int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, size_t buflen)
00080 {
00081     MQTTHeader header = {0};
00082     unsigned char* curdata = buf;
00083     unsigned char* enddata = NULL;
00084     int rc = 0;
00085     int mylen;
00086 
00087     header.byte = readChar(&curdata);
00088     *dup = header.bits.dup;
00089     *packettype = header.bits.type;
00090 
00091     curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
00092     enddata = curdata + mylen;
00093 
00094     if (enddata - curdata < 2)
00095         goto exit;
00096     *packetid = readInt(&curdata);
00097 
00098     rc = 1;
00099 exit:
00100     return rc;
00101 }
00102