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:
23:7a52009beba1
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
Jan Jongboom 23:7a52009beba1 15 * Ian Craggs - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=453144
icraggs 0:7734401cc1b4 16 *******************************************************************************/
icraggs 0:7734401cc1b4 17
icraggs 0:7734401cc1b4 18 #include "MQTTPacket.h"
icraggs 0:7734401cc1b4 19 #include "StackTrace.h"
icraggs 0:7734401cc1b4 20
icraggs 0:7734401cc1b4 21 #include <string.h>
icraggs 0:7734401cc1b4 22
icraggs 0:7734401cc1b4 23
icraggs 0:7734401cc1b4 24 /**
icraggs 0:7734401cc1b4 25 * Determines the length of the MQTT publish packet that would be produced using the supplied parameters
icraggs 0:7734401cc1b4 26 * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
icraggs 0:7734401cc1b4 27 * @param topicName the topic name to be used in the publish
icraggs 0:7734401cc1b4 28 * @param payloadlen the length of the payload to be sent
icraggs 0:7734401cc1b4 29 * @return the length of buffer needed to contain the serialized version of the packet
icraggs 0:7734401cc1b4 30 */
icraggs 0:7734401cc1b4 31 int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
icraggs 0:7734401cc1b4 32 {
icraggs 0:7734401cc1b4 33 int len = 0;
icraggs 0:7734401cc1b4 34
icraggs 0:7734401cc1b4 35 len += 2 + MQTTstrlen(topicName) + payloadlen;
icraggs 0:7734401cc1b4 36 if (qos > 0)
icraggs 0:7734401cc1b4 37 len += 2; /* packetid */
icraggs 0:7734401cc1b4 38 return len;
icraggs 0:7734401cc1b4 39 }
icraggs 0:7734401cc1b4 40
icraggs 0:7734401cc1b4 41
icraggs 0:7734401cc1b4 42 /**
icraggs 0:7734401cc1b4 43 * Serializes the supplied publish data into the supplied buffer, ready for sending
icraggs 0:7734401cc1b4 44 * @param buf the buffer into which the packet will be serialized
icraggs 0:7734401cc1b4 45 * @param buflen the length in bytes of the supplied buffer
icraggs 0:7734401cc1b4 46 * @param dup integer - the MQTT dup flag
icraggs 0:7734401cc1b4 47 * @param qos integer - the MQTT QoS value
icraggs 0:7734401cc1b4 48 * @param retained integer - the MQTT retained flag
icraggs 0:7734401cc1b4 49 * @param packetid integer - the MQTT packet identifier
icraggs 0:7734401cc1b4 50 * @param topicName MQTTString - the MQTT topic in the publish
icraggs 0:7734401cc1b4 51 * @param payload byte buffer - the MQTT publish payload
icraggs 0:7734401cc1b4 52 * @param payloadlen integer - the length of the MQTT payload
icraggs 0:7734401cc1b4 53 * @return the length of the serialized data. <= 0 indicates error
icraggs 0:7734401cc1b4 54 */
Ian Craggs 14:c2052aee81de 55 int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
Ian Craggs 14:c2052aee81de 56 MQTTString topicName, unsigned char* payload, int payloadlen)
icraggs 0:7734401cc1b4 57 {
Ian Craggs 14:c2052aee81de 58 unsigned char *ptr = buf;
icraggs 17:c5bd28cc139a 59 MQTTHeader header = {0};
icraggs 0:7734401cc1b4 60 int rem_len = 0;
icraggs 0:7734401cc1b4 61 int rc = 0;
icraggs 0:7734401cc1b4 62
icraggs 0:7734401cc1b4 63 FUNC_ENTRY;
icraggs 0:7734401cc1b4 64 if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
icraggs 0:7734401cc1b4 65 {
icraggs 0:7734401cc1b4 66 rc = MQTTPACKET_BUFFER_TOO_SHORT;
icraggs 0:7734401cc1b4 67 goto exit;
icraggs 0:7734401cc1b4 68 }
icraggs 0:7734401cc1b4 69
icraggs 0:7734401cc1b4 70 header.bits.type = PUBLISH;
icraggs 0:7734401cc1b4 71 header.bits.dup = dup;
icraggs 0:7734401cc1b4 72 header.bits.qos = qos;
icraggs 0:7734401cc1b4 73 header.bits.retain = retained;
icraggs 0:7734401cc1b4 74 writeChar(&ptr, header.byte); /* write header */
icraggs 0:7734401cc1b4 75
icraggs 0:7734401cc1b4 76 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
icraggs 0:7734401cc1b4 77
icraggs 0:7734401cc1b4 78 writeMQTTString(&ptr, topicName);
icraggs 0:7734401cc1b4 79
icraggs 0:7734401cc1b4 80 if (qos > 0)
icraggs 0:7734401cc1b4 81 writeInt(&ptr, packetid);
icraggs 0:7734401cc1b4 82
icraggs 0:7734401cc1b4 83 memcpy(ptr, payload, payloadlen);
icraggs 0:7734401cc1b4 84 ptr += payloadlen;
icraggs 0:7734401cc1b4 85
icraggs 0:7734401cc1b4 86 rc = ptr - buf;
icraggs 0:7734401cc1b4 87
icraggs 0:7734401cc1b4 88 exit:
icraggs 0:7734401cc1b4 89 FUNC_EXIT_RC(rc);
icraggs 0:7734401cc1b4 90 return rc;
icraggs 0:7734401cc1b4 91 }
icraggs 0:7734401cc1b4 92
icraggs 0:7734401cc1b4 93
icraggs 0:7734401cc1b4 94
icraggs 0:7734401cc1b4 95 /**
icraggs 0:7734401cc1b4 96 * Serializes the ack packet into the supplied buffer.
icraggs 0:7734401cc1b4 97 * @param buf the buffer into which the packet will be serialized
icraggs 0:7734401cc1b4 98 * @param buflen the length in bytes of the supplied buffer
Ian Craggs 14:c2052aee81de 99 * @param type the MQTT packet type
Ian Craggs 14:c2052aee81de 100 * @param dup the MQTT dup flag
Ian Craggs 14:c2052aee81de 101 * @param packetid the MQTT packet identifier
icraggs 0:7734401cc1b4 102 * @return serialized length, or error if 0
icraggs 0:7734401cc1b4 103 */
Ian Craggs 14:c2052aee81de 104 int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
icraggs 0:7734401cc1b4 105 {
icraggs 17:c5bd28cc139a 106 MQTTHeader header = {0};
icraggs 0:7734401cc1b4 107 int rc = 0;
Ian Craggs 14:c2052aee81de 108 unsigned char *ptr = buf;
icraggs 0:7734401cc1b4 109
icraggs 0:7734401cc1b4 110 FUNC_ENTRY;
icraggs 0:7734401cc1b4 111 if (buflen < 4)
icraggs 0:7734401cc1b4 112 {
icraggs 0:7734401cc1b4 113 rc = MQTTPACKET_BUFFER_TOO_SHORT;
icraggs 0:7734401cc1b4 114 goto exit;
icraggs 0:7734401cc1b4 115 }
Ian Craggs 14:c2052aee81de 116 header.bits.type = packettype;
icraggs 0:7734401cc1b4 117 header.bits.dup = dup;
Jan Jongboom 23:7a52009beba1 118 header.bits.qos = (packettype == PUBREL) ? 1 : 0;
icraggs 0:7734401cc1b4 119 writeChar(&ptr, header.byte); /* write header */
icraggs 0:7734401cc1b4 120
icraggs 0:7734401cc1b4 121 ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
icraggs 0:7734401cc1b4 122 writeInt(&ptr, packetid);
icraggs 0:7734401cc1b4 123 rc = ptr - buf;
icraggs 0:7734401cc1b4 124 exit:
icraggs 0:7734401cc1b4 125 FUNC_EXIT_RC(rc);
icraggs 0:7734401cc1b4 126 return rc;
icraggs 0:7734401cc1b4 127 }
icraggs 0:7734401cc1b4 128
icraggs 0:7734401cc1b4 129
icraggs 0:7734401cc1b4 130 /**
icraggs 0:7734401cc1b4 131 * Serializes a puback packet into the supplied buffer.
icraggs 0:7734401cc1b4 132 * @param buf the buffer into which the packet will be serialized
icraggs 0:7734401cc1b4 133 * @param buflen the length in bytes of the supplied buffer
icraggs 0:7734401cc1b4 134 * @param packetid integer - the MQTT packet identifier
icraggs 0:7734401cc1b4 135 * @return serialized length, or error if 0
icraggs 0:7734401cc1b4 136 */
Ian Craggs 14:c2052aee81de 137 int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid)
icraggs 0:7734401cc1b4 138 {
Jan Jongboom 23:7a52009beba1 139 return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid);
icraggs 0:7734401cc1b4 140 }
icraggs 0:7734401cc1b4 141
icraggs 0:7734401cc1b4 142
icraggs 0:7734401cc1b4 143 /**
icraggs 0:7734401cc1b4 144 * Serializes a pubrel packet into the supplied buffer.
icraggs 0:7734401cc1b4 145 * @param buf the buffer into which the packet will be serialized
icraggs 0:7734401cc1b4 146 * @param buflen the length in bytes of the supplied buffer
icraggs 0:7734401cc1b4 147 * @param dup integer - the MQTT dup flag
icraggs 0:7734401cc1b4 148 * @param packetid integer - the MQTT packet identifier
icraggs 0:7734401cc1b4 149 * @return serialized length, or error if 0
icraggs 0:7734401cc1b4 150 */
Ian Craggs 14:c2052aee81de 151 int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid)
icraggs 0:7734401cc1b4 152 {
Jan Jongboom 23:7a52009beba1 153 return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid);
icraggs 0:7734401cc1b4 154 }
icraggs 0:7734401cc1b4 155
icraggs 0:7734401cc1b4 156
icraggs 0:7734401cc1b4 157 /**
icraggs 0:7734401cc1b4 158 * Serializes a pubrel packet into the supplied buffer.
icraggs 0:7734401cc1b4 159 * @param buf the buffer into which the packet will be serialized
icraggs 0:7734401cc1b4 160 * @param buflen the length in bytes of the supplied buffer
icraggs 0:7734401cc1b4 161 * @param packetid integer - the MQTT packet identifier
icraggs 0:7734401cc1b4 162 * @return serialized length, or error if 0
icraggs 0:7734401cc1b4 163 */
Ian Craggs 14:c2052aee81de 164 int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid)
icraggs 0:7734401cc1b4 165 {
Jan Jongboom 23:7a52009beba1 166 return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid);
icraggs 0:7734401cc1b4 167 }
icraggs 0:7734401cc1b4 168