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:
icraggs
Date:
Tue Feb 04 22:31:32 2014 +0000
Revision:
0:7734401cc1b4
Child:
12:cd99ac9cb25a
First version of MQTT library

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