Fork of my original MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:43:14 2017 +0000
Revision:
0:a1734fe1ec4b
Initial commit

Who changed what in which revision?

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