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 * Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters
vpcola 0:a1734fe1ec4b 24 * @param count the number of topic filter strings in topicFilters
vpcola 0:a1734fe1ec4b 25 * @param topicFilters the array of topic filter strings to be used in the publish
vpcola 0:a1734fe1ec4b 26 * @return the length of buffer needed to contain the serialized version of the packet
vpcola 0:a1734fe1ec4b 27 */
vpcola 0:a1734fe1ec4b 28 int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[])
vpcola 0:a1734fe1ec4b 29 {
vpcola 0:a1734fe1ec4b 30 int i;
vpcola 0:a1734fe1ec4b 31 int len = 2; /* packetid */
vpcola 0:a1734fe1ec4b 32
vpcola 0:a1734fe1ec4b 33 for (i = 0; i < count; ++i)
vpcola 0:a1734fe1ec4b 34 len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/
vpcola 0:a1734fe1ec4b 35 return len;
vpcola 0:a1734fe1ec4b 36 }
vpcola 0:a1734fe1ec4b 37
vpcola 0:a1734fe1ec4b 38
vpcola 0:a1734fe1ec4b 39 /**
vpcola 0:a1734fe1ec4b 40 * Serializes the supplied unsubscribe data into the supplied buffer, ready for sending
vpcola 0:a1734fe1ec4b 41 * @param buf the raw buffer data, of the correct length determined by the remaining length field
vpcola 0:a1734fe1ec4b 42 * @param buflen the length in bytes of the data in the supplied buffer
vpcola 0:a1734fe1ec4b 43 * @param dup integer - the MQTT dup flag
vpcola 0:a1734fe1ec4b 44 * @param packetid integer - the MQTT packet identifier
vpcola 0:a1734fe1ec4b 45 * @param count - number of members in the topicFilters array
vpcola 0:a1734fe1ec4b 46 * @param topicFilters - array of topic filter names
vpcola 0:a1734fe1ec4b 47 * @return the length of the serialized data. <= 0 indicates error
vpcola 0:a1734fe1ec4b 48 */
vpcola 0:a1734fe1ec4b 49 int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
vpcola 0:a1734fe1ec4b 50 int count, MQTTString topicFilters[])
vpcola 0:a1734fe1ec4b 51 {
vpcola 0:a1734fe1ec4b 52 unsigned char *ptr = buf;
vpcola 0:a1734fe1ec4b 53 MQTTHeader header = {0};
vpcola 0:a1734fe1ec4b 54 int rem_len = 0;
vpcola 0:a1734fe1ec4b 55 int rc = -1;
vpcola 0:a1734fe1ec4b 56 int i = 0;
vpcola 0:a1734fe1ec4b 57
vpcola 0:a1734fe1ec4b 58 FUNC_ENTRY;
vpcola 0:a1734fe1ec4b 59 if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen)
vpcola 0:a1734fe1ec4b 60 {
vpcola 0:a1734fe1ec4b 61 rc = MQTTPACKET_BUFFER_TOO_SHORT;
vpcola 0:a1734fe1ec4b 62 goto exit;
vpcola 0:a1734fe1ec4b 63 }
vpcola 0:a1734fe1ec4b 64
vpcola 0:a1734fe1ec4b 65 header.byte = 0;
vpcola 0:a1734fe1ec4b 66 header.bits.type = UNSUBSCRIBE;
vpcola 0:a1734fe1ec4b 67 header.bits.dup = dup;
vpcola 0:a1734fe1ec4b 68 header.bits.qos = 1;
vpcola 0:a1734fe1ec4b 69 writeChar(&ptr, header.byte); /* write header */
vpcola 0:a1734fe1ec4b 70
vpcola 0:a1734fe1ec4b 71 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
vpcola 0:a1734fe1ec4b 72
vpcola 0:a1734fe1ec4b 73 writeInt(&ptr, packetid);
vpcola 0:a1734fe1ec4b 74
vpcola 0:a1734fe1ec4b 75 for (i = 0; i < count; ++i)
vpcola 0:a1734fe1ec4b 76 writeMQTTString(&ptr, topicFilters[i]);
vpcola 0:a1734fe1ec4b 77
vpcola 0:a1734fe1ec4b 78 rc = ptr - buf;
vpcola 0:a1734fe1ec4b 79 exit:
vpcola 0:a1734fe1ec4b 80 FUNC_EXIT_RC(rc);
vpcola 0:a1734fe1ec4b 81 return rc;
vpcola 0:a1734fe1ec4b 82 }
vpcola 0:a1734fe1ec4b 83
vpcola 0:a1734fe1ec4b 84
vpcola 0:a1734fe1ec4b 85 /**
vpcola 0:a1734fe1ec4b 86 * Deserializes the supplied (wire) buffer into unsuback data
vpcola 0:a1734fe1ec4b 87 * @param packetid returned integer - the MQTT packet identifier
vpcola 0:a1734fe1ec4b 88 * @param buf the raw buffer data, of the correct length determined by the remaining length field
vpcola 0:a1734fe1ec4b 89 * @param buflen the length in bytes of the data in the supplied buffer
vpcola 0:a1734fe1ec4b 90 * @return error code. 1 is success, 0 is failure
vpcola 0:a1734fe1ec4b 91 */
vpcola 0:a1734fe1ec4b 92 int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen)
vpcola 0:a1734fe1ec4b 93 {
vpcola 0:a1734fe1ec4b 94 unsigned char type = 0;
vpcola 0:a1734fe1ec4b 95 unsigned char dup = 0;
vpcola 0:a1734fe1ec4b 96 int rc = 0;
vpcola 0:a1734fe1ec4b 97
vpcola 0:a1734fe1ec4b 98 FUNC_ENTRY;
vpcola 0:a1734fe1ec4b 99 rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
vpcola 0:a1734fe1ec4b 100 if (type == UNSUBACK)
vpcola 0:a1734fe1ec4b 101 rc = 1;
vpcola 0:a1734fe1ec4b 102 FUNC_EXIT_RC(rc);
vpcola 0:a1734fe1ec4b 103 return rc;
vpcola 0:a1734fe1ec4b 104 }
vpcola 0:a1734fe1ec4b 105
vpcola 0:a1734fe1ec4b 106