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