Basic C library for MQTT packet serialization and deserialization

Dependents:   MQTT MQTT MQTT MQTT ... more

Fork of MQTTPacket by MQTT

This library is part of the EclipseTM Paho project; specifically the embedded client.

A basic MQTT library in C for packet serialization and deserialization

Committer:
Ian Craggs
Date:
Mon Oct 30 12:47:11 2017 +0000
Revision:
25:aedcaf7984d5
Parent:
17:c5bd28cc139a
Merging mine and Jan's changes

Who changed what in which revision?

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