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 "MQTTPacket.h"
vpcola 0:a1734fe1ec4b 18 #include "StackTrace.h"
vpcola 0:a1734fe1ec4b 19
vpcola 0:a1734fe1ec4b 20 #include <string.h>
vpcola 0:a1734fe1ec4b 21
vpcola 0:a1734fe1ec4b 22
vpcola 0:a1734fe1ec4b 23 /**
vpcola 0:a1734fe1ec4b 24 * Deserializes the supplied (wire) buffer into subscribe data
vpcola 0:a1734fe1ec4b 25 * @param dup integer returned - the MQTT dup flag
vpcola 0:a1734fe1ec4b 26 * @param packetid integer returned - the MQTT packet identifier
vpcola 0:a1734fe1ec4b 27 * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
vpcola 0:a1734fe1ec4b 28 * @param count - number of members in the topicFilters and requestedQoSs arrays
vpcola 0:a1734fe1ec4b 29 * @param topicFilters - array of topic filter names
vpcola 0:a1734fe1ec4b 30 * @param requestedQoSs - array of requested QoS
vpcola 0:a1734fe1ec4b 31 * @param buf the raw buffer data, of the correct length determined by the remaining length field
vpcola 0:a1734fe1ec4b 32 * @param buflen the length in bytes of the data in the supplied buffer
vpcola 0:a1734fe1ec4b 33 * @return the length of the serialized data. <= 0 indicates error
vpcola 0:a1734fe1ec4b 34 */
vpcola 0:a1734fe1ec4b 35 int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
vpcola 0:a1734fe1ec4b 36 int requestedQoSs[], unsigned char* buf, int buflen)
vpcola 0:a1734fe1ec4b 37 {
vpcola 0:a1734fe1ec4b 38 MQTTHeader header = {0};
vpcola 0:a1734fe1ec4b 39 unsigned char* curdata = buf;
vpcola 0:a1734fe1ec4b 40 unsigned char* enddata = NULL;
vpcola 0:a1734fe1ec4b 41 int rc = -1;
vpcola 0:a1734fe1ec4b 42 int mylen = 0;
vpcola 0:a1734fe1ec4b 43
vpcola 0:a1734fe1ec4b 44 FUNC_ENTRY;
vpcola 0:a1734fe1ec4b 45 header.byte = readChar(&curdata);
vpcola 0:a1734fe1ec4b 46 if (header.bits.type != SUBSCRIBE)
vpcola 0:a1734fe1ec4b 47 goto exit;
vpcola 0:a1734fe1ec4b 48 *dup = header.bits.dup;
vpcola 0:a1734fe1ec4b 49
vpcola 0:a1734fe1ec4b 50 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
vpcola 0:a1734fe1ec4b 51 enddata = curdata + mylen;
vpcola 0:a1734fe1ec4b 52
vpcola 0:a1734fe1ec4b 53 *packetid = readInt(&curdata);
vpcola 0:a1734fe1ec4b 54
vpcola 0:a1734fe1ec4b 55 *count = 0;
vpcola 0:a1734fe1ec4b 56 while (curdata < enddata)
vpcola 0:a1734fe1ec4b 57 {
vpcola 0:a1734fe1ec4b 58 if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
vpcola 0:a1734fe1ec4b 59 goto exit;
vpcola 0:a1734fe1ec4b 60 if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */
vpcola 0:a1734fe1ec4b 61 goto exit;
vpcola 0:a1734fe1ec4b 62 requestedQoSs[*count] = readChar(&curdata);
vpcola 0:a1734fe1ec4b 63 (*count)++;
vpcola 0:a1734fe1ec4b 64 }
vpcola 0:a1734fe1ec4b 65
vpcola 0:a1734fe1ec4b 66 rc = 1;
vpcola 0:a1734fe1ec4b 67 exit:
vpcola 0:a1734fe1ec4b 68 FUNC_EXIT_RC(rc);
vpcola 0:a1734fe1ec4b 69 return rc;
vpcola 0:a1734fe1ec4b 70 }
vpcola 0:a1734fe1ec4b 71
vpcola 0:a1734fe1ec4b 72
vpcola 0:a1734fe1ec4b 73 /**
vpcola 0:a1734fe1ec4b 74 * Serializes the supplied suback data into the supplied buffer, ready for sending
vpcola 0:a1734fe1ec4b 75 * @param buf the buffer into which the packet will be serialized
vpcola 0:a1734fe1ec4b 76 * @param buflen the length in bytes of the supplied buffer
vpcola 0:a1734fe1ec4b 77 * @param packetid integer - the MQTT packet identifier
vpcola 0:a1734fe1ec4b 78 * @param count - number of members in the grantedQoSs array
vpcola 0:a1734fe1ec4b 79 * @param grantedQoSs - array of granted QoS
vpcola 0:a1734fe1ec4b 80 * @return the length of the serialized data. <= 0 indicates error
vpcola 0:a1734fe1ec4b 81 */
vpcola 0:a1734fe1ec4b 82 int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs)
vpcola 0:a1734fe1ec4b 83 {
vpcola 0:a1734fe1ec4b 84 MQTTHeader header = {0};
vpcola 0:a1734fe1ec4b 85 int rc = -1;
vpcola 0:a1734fe1ec4b 86 unsigned char *ptr = buf;
vpcola 0:a1734fe1ec4b 87 int i;
vpcola 0:a1734fe1ec4b 88
vpcola 0:a1734fe1ec4b 89 FUNC_ENTRY;
vpcola 0:a1734fe1ec4b 90 if (buflen < 2 + count)
vpcola 0:a1734fe1ec4b 91 {
vpcola 0:a1734fe1ec4b 92 rc = MQTTPACKET_BUFFER_TOO_SHORT;
vpcola 0:a1734fe1ec4b 93 goto exit;
vpcola 0:a1734fe1ec4b 94 }
vpcola 0:a1734fe1ec4b 95 header.byte = 0;
vpcola 0:a1734fe1ec4b 96 header.bits.type = SUBACK;
vpcola 0:a1734fe1ec4b 97 writeChar(&ptr, header.byte); /* write header */
vpcola 0:a1734fe1ec4b 98
vpcola 0:a1734fe1ec4b 99 ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */
vpcola 0:a1734fe1ec4b 100
vpcola 0:a1734fe1ec4b 101 writeInt(&ptr, packetid);
vpcola 0:a1734fe1ec4b 102
vpcola 0:a1734fe1ec4b 103 for (i = 0; i < count; ++i)
vpcola 0:a1734fe1ec4b 104 writeChar(&ptr, grantedQoSs[i]);
vpcola 0:a1734fe1ec4b 105
vpcola 0:a1734fe1ec4b 106 rc = ptr - buf;
vpcola 0:a1734fe1ec4b 107 exit:
vpcola 0:a1734fe1ec4b 108 FUNC_EXIT_RC(rc);
vpcola 0:a1734fe1ec4b 109 return rc;
vpcola 0:a1734fe1ec4b 110 }
vpcola 0:a1734fe1ec4b 111
vpcola 0:a1734fe1ec4b 112