Version to make it easier to reuse without source modifications

Committer:
JMF
Date:
Tue Mar 27 17:26:35 2018 +0000
Revision:
0:5cd4781e0c88
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 0:5cd4781e0c88 1 /*******************************************************************************
JMF 0:5cd4781e0c88 2 * Copyright (c) 2014 IBM Corp.
JMF 0:5cd4781e0c88 3 *
JMF 0:5cd4781e0c88 4 * All rights reserved. This program and the accompanying materials
JMF 0:5cd4781e0c88 5 * are made available under the terms of the Eclipse Public License v1.0
JMF 0:5cd4781e0c88 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
JMF 0:5cd4781e0c88 7 *
JMF 0:5cd4781e0c88 8 * The Eclipse Public License is available at
JMF 0:5cd4781e0c88 9 * http://www.eclipse.org/legal/epl-v10.html
JMF 0:5cd4781e0c88 10 * and the Eclipse Distribution License is available at
JMF 0:5cd4781e0c88 11 * http://www.eclipse.org/org/documents/edl-v10.php.
JMF 0:5cd4781e0c88 12 *
JMF 0:5cd4781e0c88 13 * Contributors:
JMF 0:5cd4781e0c88 14 * Ian Craggs - initial API and implementation and/or initial documentation
JMF 0:5cd4781e0c88 15 *******************************************************************************/
JMF 0:5cd4781e0c88 16
JMF 0:5cd4781e0c88 17 #include "StackTrace.h"
JMF 0:5cd4781e0c88 18 #include "MQTTPacket.h"
JMF 0:5cd4781e0c88 19 #include <string.h>
JMF 0:5cd4781e0c88 20
JMF 0:5cd4781e0c88 21 #define min(a, b) ((a < b) ? a : b)
JMF 0:5cd4781e0c88 22
JMF 0:5cd4781e0c88 23
JMF 0:5cd4781e0c88 24 /**
JMF 0:5cd4781e0c88 25 * Validates MQTT protocol name and version combinations
JMF 0:5cd4781e0c88 26 * @param protocol the MQTT protocol name as an MQTTString
JMF 0:5cd4781e0c88 27 * @param version the MQTT protocol version number, as in the connect packet
JMF 0:5cd4781e0c88 28 * @return correct MQTT combination? 1 is true, 0 is false
JMF 0:5cd4781e0c88 29 */
JMF 0:5cd4781e0c88 30 int MQTTPacket_checkVersion(MQTTString* protocol, int version)
JMF 0:5cd4781e0c88 31 {
JMF 0:5cd4781e0c88 32 int rc = 0;
JMF 0:5cd4781e0c88 33
JMF 0:5cd4781e0c88 34 if (version == 3 && memcmp(protocol->lenstring.data, "MQIdsp",
JMF 0:5cd4781e0c88 35 min(6, protocol->lenstring.len)) == 0)
JMF 0:5cd4781e0c88 36 rc = 1;
JMF 0:5cd4781e0c88 37 else if (version == 4 && memcmp(protocol->lenstring.data, "MQTT",
JMF 0:5cd4781e0c88 38 min(4, protocol->lenstring.len)) == 0)
JMF 0:5cd4781e0c88 39 rc = 1;
JMF 0:5cd4781e0c88 40 return rc;
JMF 0:5cd4781e0c88 41 }
JMF 0:5cd4781e0c88 42
JMF 0:5cd4781e0c88 43
JMF 0:5cd4781e0c88 44 /**
JMF 0:5cd4781e0c88 45 * Deserializes the supplied (wire) buffer into connect data structure
JMF 0:5cd4781e0c88 46 * @param data the connect data structure to be filled out
JMF 0:5cd4781e0c88 47 * @param buf the raw buffer data, of the correct length determined by the remaining length field
JMF 0:5cd4781e0c88 48 * @param len the length in bytes of the data in the supplied buffer
JMF 0:5cd4781e0c88 49 * @return error code. 1 is success, 0 is failure
JMF 0:5cd4781e0c88 50 */
JMF 0:5cd4781e0c88 51 int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len)
JMF 0:5cd4781e0c88 52 {
JMF 0:5cd4781e0c88 53 MQTTHeader header = {0};
JMF 0:5cd4781e0c88 54 MQTTConnectFlags flags = {0};
JMF 0:5cd4781e0c88 55 unsigned char* curdata = buf;
JMF 0:5cd4781e0c88 56 unsigned char* enddata = &buf[len];
JMF 0:5cd4781e0c88 57 int rc = 0;
JMF 0:5cd4781e0c88 58 MQTTString Protocol;
JMF 0:5cd4781e0c88 59 int version;
JMF 0:5cd4781e0c88 60 int mylen = 0;
JMF 0:5cd4781e0c88 61
JMF 0:5cd4781e0c88 62 FUNC_ENTRY;
JMF 0:5cd4781e0c88 63 header.byte = readChar(&curdata);
JMF 0:5cd4781e0c88 64 if (header.bits.type != CONNECT)
JMF 0:5cd4781e0c88 65 goto exit;
JMF 0:5cd4781e0c88 66
JMF 0:5cd4781e0c88 67 curdata += MQTTPacket_decodeBuf(curdata, &mylen); /* read remaining length */
JMF 0:5cd4781e0c88 68
JMF 0:5cd4781e0c88 69 if (!readMQTTLenString(&Protocol, &curdata, enddata) ||
JMF 0:5cd4781e0c88 70 enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
JMF 0:5cd4781e0c88 71 goto exit;
JMF 0:5cd4781e0c88 72
JMF 0:5cd4781e0c88 73 version = (int)readChar(&curdata); /* Protocol version */
JMF 0:5cd4781e0c88 74 /* If we don't recognize the protocol version, we don't parse the connect packet on the
JMF 0:5cd4781e0c88 75 * basis that we don't know what the format will be.
JMF 0:5cd4781e0c88 76 */
JMF 0:5cd4781e0c88 77 if (MQTTPacket_checkVersion(&Protocol, version))
JMF 0:5cd4781e0c88 78 {
JMF 0:5cd4781e0c88 79 flags.all = readChar(&curdata);
JMF 0:5cd4781e0c88 80 data->cleansession = flags.bits.cleansession;
JMF 0:5cd4781e0c88 81 data->keepAliveInterval = readInt(&curdata);
JMF 0:5cd4781e0c88 82 if (!readMQTTLenString(&data->clientID, &curdata, enddata))
JMF 0:5cd4781e0c88 83 goto exit;
JMF 0:5cd4781e0c88 84 if (flags.bits.will)
JMF 0:5cd4781e0c88 85 {
JMF 0:5cd4781e0c88 86 data->willFlag = 1;
JMF 0:5cd4781e0c88 87 data->will.qos = flags.bits.willQoS;
JMF 0:5cd4781e0c88 88 data->will.retained = flags.bits.willRetain;
JMF 0:5cd4781e0c88 89 if (!readMQTTLenString(&data->will.topicName, &curdata, enddata) ||
JMF 0:5cd4781e0c88 90 !readMQTTLenString(&data->will.message, &curdata, enddata))
JMF 0:5cd4781e0c88 91 goto exit;
JMF 0:5cd4781e0c88 92 }
JMF 0:5cd4781e0c88 93 if (flags.bits.username)
JMF 0:5cd4781e0c88 94 {
JMF 0:5cd4781e0c88 95 if (enddata - curdata < 3 || !readMQTTLenString(&data->username, &curdata, enddata))
JMF 0:5cd4781e0c88 96 goto exit; /* username flag set, but no username supplied - invalid */
JMF 0:5cd4781e0c88 97 if (flags.bits.password &&
JMF 0:5cd4781e0c88 98 (enddata - curdata < 3 || !readMQTTLenString(&data->password, &curdata, enddata)))
JMF 0:5cd4781e0c88 99 goto exit; /* password flag set, but no password supplied - invalid */
JMF 0:5cd4781e0c88 100 }
JMF 0:5cd4781e0c88 101 else if (flags.bits.password)
JMF 0:5cd4781e0c88 102 goto exit; /* password flag set without username - invalid */
JMF 0:5cd4781e0c88 103 rc = 1;
JMF 0:5cd4781e0c88 104 }
JMF 0:5cd4781e0c88 105 exit:
JMF 0:5cd4781e0c88 106 FUNC_EXIT_RC(rc);
JMF 0:5cd4781e0c88 107 return rc;
JMF 0:5cd4781e0c88 108 }
JMF 0:5cd4781e0c88 109
JMF 0:5cd4781e0c88 110
JMF 0:5cd4781e0c88 111 /**
JMF 0:5cd4781e0c88 112 * Serializes the connack packet into the supplied buffer.
JMF 0:5cd4781e0c88 113 * @param buf the buffer into which the packet will be serialized
JMF 0:5cd4781e0c88 114 * @param buflen the length in bytes of the supplied buffer
JMF 0:5cd4781e0c88 115 * @param connack_rc the integer connack return code to be used
JMF 0:5cd4781e0c88 116 * @param sessionPresent the MQTT 3.1.1 sessionPresent flag
JMF 0:5cd4781e0c88 117 * @return serialized length, or error if 0
JMF 0:5cd4781e0c88 118 */
JMF 0:5cd4781e0c88 119 int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent)
JMF 0:5cd4781e0c88 120 {
JMF 0:5cd4781e0c88 121 MQTTHeader header = {0};
JMF 0:5cd4781e0c88 122 int rc = 0;
JMF 0:5cd4781e0c88 123 unsigned char *ptr = buf;
JMF 0:5cd4781e0c88 124 MQTTConnackFlags flags = {0};
JMF 0:5cd4781e0c88 125
JMF 0:5cd4781e0c88 126 FUNC_ENTRY;
JMF 0:5cd4781e0c88 127 if (buflen < 2)
JMF 0:5cd4781e0c88 128 {
JMF 0:5cd4781e0c88 129 rc = MQTTPACKET_BUFFER_TOO_SHORT;
JMF 0:5cd4781e0c88 130 goto exit;
JMF 0:5cd4781e0c88 131 }
JMF 0:5cd4781e0c88 132 header.byte = 0;
JMF 0:5cd4781e0c88 133 header.bits.type = CONNACK;
JMF 0:5cd4781e0c88 134 writeChar(&ptr, header.byte); /* write header */
JMF 0:5cd4781e0c88 135
JMF 0:5cd4781e0c88 136 ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
JMF 0:5cd4781e0c88 137
JMF 0:5cd4781e0c88 138 flags.all = 0;
JMF 0:5cd4781e0c88 139 flags.bits.sessionpresent = sessionPresent;
JMF 0:5cd4781e0c88 140 writeChar(&ptr, flags.all);
JMF 0:5cd4781e0c88 141 writeChar(&ptr, connack_rc);
JMF 0:5cd4781e0c88 142
JMF 0:5cd4781e0c88 143 rc = ptr - buf;
JMF 0:5cd4781e0c88 144 exit:
JMF 0:5cd4781e0c88 145 FUNC_EXIT_RC(rc);
JMF 0:5cd4781e0c88 146 return rc;
JMF 0:5cd4781e0c88 147 }
JMF 0:5cd4781e0c88 148