Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

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