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:
Mon Aug 03 12:38:58 2015 +0000
Revision:
21:62396c1620b6
Parent:
17:c5bd28cc139a
Merge updates

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 * Determines the length of the MQTT connect packet that would be produced using the supplied connect options.
icraggs 0:7734401cc1b4 24 * @param options the options to be used to build the connect packet
icraggs 0:7734401cc1b4 25 * @return the length of buffer needed to contain the serialized version of the packet
icraggs 0:7734401cc1b4 26 */
icraggs 0:7734401cc1b4 27 int MQTTSerialize_connectLength(MQTTPacket_connectData* options)
icraggs 0:7734401cc1b4 28 {
icraggs 0:7734401cc1b4 29 int len = 0;
icraggs 0:7734401cc1b4 30
icraggs 0:7734401cc1b4 31 FUNC_ENTRY;
icraggs 0:7734401cc1b4 32
icraggs 0:7734401cc1b4 33 if (options->MQTTVersion == 3)
icraggs 0:7734401cc1b4 34 len = 12; /* variable depending on MQTT or MQIsdp */
icraggs 0:7734401cc1b4 35 else if (options->MQTTVersion == 4)
icraggs 0:7734401cc1b4 36 len = 10;
icraggs 0:7734401cc1b4 37
icraggs 0:7734401cc1b4 38 len += MQTTstrlen(options->clientID)+2;
icraggs 0:7734401cc1b4 39 if (options->willFlag)
icraggs 0:7734401cc1b4 40 len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2;
icraggs 0:7734401cc1b4 41 if (options->username.cstring || options->username.lenstring.data)
icraggs 0:7734401cc1b4 42 len += MQTTstrlen(options->username)+2;
icraggs 0:7734401cc1b4 43 if (options->password.cstring || options->password.lenstring.data)
icraggs 0:7734401cc1b4 44 len += MQTTstrlen(options->password)+2;
icraggs 0:7734401cc1b4 45
icraggs 0:7734401cc1b4 46 FUNC_EXIT_RC(len);
icraggs 0:7734401cc1b4 47 return len;
icraggs 0:7734401cc1b4 48 }
icraggs 0:7734401cc1b4 49
icraggs 0:7734401cc1b4 50
icraggs 0:7734401cc1b4 51 /**
icraggs 0:7734401cc1b4 52 * Serializes the connect options into the buffer.
icraggs 0:7734401cc1b4 53 * @param buf the buffer into which the packet will be serialized
icraggs 0:7734401cc1b4 54 * @param len the length in bytes of the supplied buffer
icraggs 0:7734401cc1b4 55 * @param options the options to be used to build the connect packet
icraggs 0:7734401cc1b4 56 * @return serialized length, or error if 0
icraggs 0:7734401cc1b4 57 */
Ian Craggs 14:c2052aee81de 58 int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options)
icraggs 0:7734401cc1b4 59 {
Ian Craggs 14:c2052aee81de 60 unsigned char *ptr = buf;
icraggs 17:c5bd28cc139a 61 MQTTHeader header = {0};
icraggs 17:c5bd28cc139a 62 MQTTConnectFlags flags = {0};
icraggs 0:7734401cc1b4 63 int len = 0;
icraggs 0:7734401cc1b4 64 int rc = -1;
icraggs 0:7734401cc1b4 65
icraggs 0:7734401cc1b4 66 FUNC_ENTRY;
icraggs 0:7734401cc1b4 67 if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen)
icraggs 0:7734401cc1b4 68 {
icraggs 0:7734401cc1b4 69 rc = MQTTPACKET_BUFFER_TOO_SHORT;
icraggs 0:7734401cc1b4 70 goto exit;
icraggs 0:7734401cc1b4 71 }
icraggs 0:7734401cc1b4 72
icraggs 0:7734401cc1b4 73 header.byte = 0;
icraggs 0:7734401cc1b4 74 header.bits.type = CONNECT;
icraggs 0:7734401cc1b4 75 writeChar(&ptr, header.byte); /* write header */
icraggs 0:7734401cc1b4 76
icraggs 0:7734401cc1b4 77 ptr += MQTTPacket_encode(ptr, len); /* write remaining length */
icraggs 0:7734401cc1b4 78
icraggs 0:7734401cc1b4 79 if (options->MQTTVersion == 4)
icraggs 0:7734401cc1b4 80 {
icraggs 0:7734401cc1b4 81 writeCString(&ptr, "MQTT");
icraggs 0:7734401cc1b4 82 writeChar(&ptr, (char) 4);
icraggs 0:7734401cc1b4 83 }
icraggs 0:7734401cc1b4 84 else
icraggs 0:7734401cc1b4 85 {
icraggs 0:7734401cc1b4 86 writeCString(&ptr, "MQIsdp");
icraggs 0:7734401cc1b4 87 writeChar(&ptr, (char) 3);
icraggs 0:7734401cc1b4 88 }
icraggs 0:7734401cc1b4 89
icraggs 0:7734401cc1b4 90 flags.all = 0;
icraggs 0:7734401cc1b4 91 flags.bits.cleansession = options->cleansession;
icraggs 0:7734401cc1b4 92 flags.bits.will = (options->willFlag) ? 1 : 0;
icraggs 0:7734401cc1b4 93 if (flags.bits.will)
icraggs 0:7734401cc1b4 94 {
icraggs 0:7734401cc1b4 95 flags.bits.willQoS = options->will.qos;
icraggs 0:7734401cc1b4 96 flags.bits.willRetain = options->will.retained;
icraggs 0:7734401cc1b4 97 }
icraggs 0:7734401cc1b4 98
icraggs 0:7734401cc1b4 99 if (options->username.cstring || options->username.lenstring.data)
icraggs 0:7734401cc1b4 100 flags.bits.username = 1;
icraggs 0:7734401cc1b4 101 if (options->password.cstring || options->password.lenstring.data)
icraggs 0:7734401cc1b4 102 flags.bits.password = 1;
icraggs 0:7734401cc1b4 103
icraggs 0:7734401cc1b4 104 writeChar(&ptr, flags.all);
icraggs 0:7734401cc1b4 105 writeInt(&ptr, options->keepAliveInterval);
icraggs 0:7734401cc1b4 106 writeMQTTString(&ptr, options->clientID);
icraggs 0:7734401cc1b4 107 if (options->willFlag)
icraggs 0:7734401cc1b4 108 {
icraggs 0:7734401cc1b4 109 writeMQTTString(&ptr, options->will.topicName);
icraggs 0:7734401cc1b4 110 writeMQTTString(&ptr, options->will.message);
icraggs 0:7734401cc1b4 111 }
icraggs 0:7734401cc1b4 112 if (flags.bits.username)
icraggs 0:7734401cc1b4 113 writeMQTTString(&ptr, options->username);
icraggs 0:7734401cc1b4 114 if (flags.bits.password)
icraggs 0:7734401cc1b4 115 writeMQTTString(&ptr, options->password);
icraggs 0:7734401cc1b4 116
icraggs 0:7734401cc1b4 117 rc = ptr - buf;
icraggs 0:7734401cc1b4 118
icraggs 0:7734401cc1b4 119 exit: FUNC_EXIT_RC(rc);
icraggs 0:7734401cc1b4 120 return rc;
icraggs 0:7734401cc1b4 121 }
icraggs 0:7734401cc1b4 122
icraggs 0:7734401cc1b4 123
icraggs 0:7734401cc1b4 124 /**
icraggs 0:7734401cc1b4 125 * Deserializes the supplied (wire) buffer into connack data - return code
icraggs 16:d0b3886ada32 126 * @param sessionPresent the session present flag returned (only for MQTT 3.1.1)
icraggs 0:7734401cc1b4 127 * @param connack_rc returned integer value of the connack return code
icraggs 0:7734401cc1b4 128 * @param buf the raw buffer data, of the correct length determined by the remaining length field
icraggs 0:7734401cc1b4 129 * @param len the length in bytes of the data in the supplied buffer
icraggs 0:7734401cc1b4 130 * @return error code. 1 is success, 0 is failure
icraggs 0:7734401cc1b4 131 */
icraggs 16:d0b3886ada32 132 int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen)
icraggs 0:7734401cc1b4 133 {
icraggs 17:c5bd28cc139a 134 MQTTHeader header = {0};
Ian Craggs 14:c2052aee81de 135 unsigned char* curdata = buf;
Ian Craggs 14:c2052aee81de 136 unsigned char* enddata = NULL;
icraggs 0:7734401cc1b4 137 int rc = 0;
icraggs 0:7734401cc1b4 138 int mylen;
icraggs 17:c5bd28cc139a 139 MQTTConnackFlags flags = {0};
icraggs 0:7734401cc1b4 140
icraggs 0:7734401cc1b4 141 FUNC_ENTRY;
icraggs 0:7734401cc1b4 142 header.byte = readChar(&curdata);
icraggs 0:7734401cc1b4 143 if (header.bits.type != CONNACK)
icraggs 0:7734401cc1b4 144 goto exit;
icraggs 0:7734401cc1b4 145
icraggs 0:7734401cc1b4 146 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
icraggs 0:7734401cc1b4 147 enddata = curdata + mylen;
icraggs 0:7734401cc1b4 148 if (enddata - curdata < 2)
icraggs 0:7734401cc1b4 149 goto exit;
icraggs 0:7734401cc1b4 150
icraggs 16:d0b3886ada32 151 flags.all = readChar(&curdata);
icraggs 16:d0b3886ada32 152 *sessionPresent = flags.bits.sessionpresent;
icraggs 0:7734401cc1b4 153 *connack_rc = readChar(&curdata);
icraggs 0:7734401cc1b4 154
icraggs 0:7734401cc1b4 155 rc = 1;
icraggs 0:7734401cc1b4 156 exit:
icraggs 0:7734401cc1b4 157 FUNC_EXIT_RC(rc);
icraggs 0:7734401cc1b4 158 return rc;
icraggs 0:7734401cc1b4 159 }
icraggs 0:7734401cc1b4 160
icraggs 0:7734401cc1b4 161
Ian Craggs 4:c502573c6016 162
icraggs 0:7734401cc1b4 163 /**
Ian Craggs 4:c502573c6016 164 * Serializes a 0-length packet into the supplied buffer, ready for writing to a socket
icraggs 0:7734401cc1b4 165 * @param buf the buffer into which the packet will be serialized
icraggs 0:7734401cc1b4 166 * @param buflen the length in bytes of the supplied buffer, to avoid overruns
Ian Craggs 14:c2052aee81de 167 * @param packettype the message type
icraggs 0:7734401cc1b4 168 * @return serialized length, or error if 0
icraggs 0:7734401cc1b4 169 */
Ian Craggs 14:c2052aee81de 170 int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype)
icraggs 0:7734401cc1b4 171 {
icraggs 17:c5bd28cc139a 172 MQTTHeader header = {0};
icraggs 0:7734401cc1b4 173 int rc = -1;
Ian Craggs 14:c2052aee81de 174 unsigned char *ptr = buf;
icraggs 0:7734401cc1b4 175
icraggs 0:7734401cc1b4 176 FUNC_ENTRY;
icraggs 0:7734401cc1b4 177 if (buflen < 2)
icraggs 0:7734401cc1b4 178 {
icraggs 0:7734401cc1b4 179 rc = MQTTPACKET_BUFFER_TOO_SHORT;
icraggs 0:7734401cc1b4 180 goto exit;
icraggs 0:7734401cc1b4 181 }
icraggs 0:7734401cc1b4 182 header.byte = 0;
Ian Craggs 14:c2052aee81de 183 header.bits.type = packettype;
icraggs 0:7734401cc1b4 184 writeChar(&ptr, header.byte); /* write header */
icraggs 0:7734401cc1b4 185
icraggs 0:7734401cc1b4 186 ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */
icraggs 0:7734401cc1b4 187 rc = ptr - buf;
icraggs 0:7734401cc1b4 188 exit:
icraggs 0:7734401cc1b4 189 FUNC_EXIT_RC(rc);
icraggs 0:7734401cc1b4 190 return rc;
icraggs 0:7734401cc1b4 191 }
Ian Craggs 4:c502573c6016 192
Ian Craggs 4:c502573c6016 193
Ian Craggs 4:c502573c6016 194 /**
Ian Craggs 4:c502573c6016 195 * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
Ian Craggs 4:c502573c6016 196 * @param buf the buffer into which the packet will be serialized
Ian Craggs 4:c502573c6016 197 * @param buflen the length in bytes of the supplied buffer, to avoid overruns
Ian Craggs 4:c502573c6016 198 * @return serialized length, or error if 0
Ian Craggs 4:c502573c6016 199 */
Ian Craggs 14:c2052aee81de 200 int MQTTSerialize_disconnect(unsigned char* buf, int buflen)
Ian Craggs 4:c502573c6016 201 {
Ian Craggs 4:c502573c6016 202 return MQTTSerialize_zero(buf, buflen, DISCONNECT);
Ian Craggs 4:c502573c6016 203 }
Ian Craggs 4:c502573c6016 204
Ian Craggs 4:c502573c6016 205
Ian Craggs 4:c502573c6016 206 /**
Ian Craggs 4:c502573c6016 207 * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
Ian Craggs 4:c502573c6016 208 * @param buf the buffer into which the packet will be serialized
Ian Craggs 4:c502573c6016 209 * @param buflen the length in bytes of the supplied buffer, to avoid overruns
Ian Craggs 4:c502573c6016 210 * @return serialized length, or error if 0
Ian Craggs 4:c502573c6016 211 */
Ian Craggs 14:c2052aee81de 212 int MQTTSerialize_pingreq(unsigned char* buf, int buflen)
Ian Craggs 4:c502573c6016 213 {
Ian Craggs 4:c502573c6016 214 return MQTTSerialize_zero(buf, buflen, PINGREQ);
Ian Craggs 4:c502573c6016 215 }