1 /*******************************************************************************
2 * Copyright (c) 2014 IBM Corp.
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * and Eclipse Distribution License v1.0 which accompany this distribution.
7 *
8 * The Eclipse Public License is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 * and the Eclipse Distribution License is available at
11 * http://www.eclipse.org/org/documents/edl-v10.php.
12 *
13 * Contributors:
14 * Ian Craggs - initial API and implementation and/or initial documentation
15 *******************************************************************************/
16
17 #include "MQTTPacket.h"
18 #include "StackTrace.h"
19
20 #include <string.h>
21
22 /**
23 * Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters
24 * @param count the number of topic filter strings in topicFilters
25 * @param topicFilters the array of topic filter strings to be used in the publish
26 * @return the length of buffer needed to contain the serialized version of the packet
27 */
MQTTSerialize_unsubscribeLength(int count,MQTTString topicFilters[])28 int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[])
29 {
30 int i;
31 int len = 2; /* packetid */
32
33 for (i = 0; i < count; ++i)
34 len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/
35 return len;
36 }
37
38
39 /**
40 * Serializes the supplied unsubscribe data into the supplied buffer, ready for sending
41 * @param buf the raw buffer data, of the correct length determined by the remaining length field
42 * @param buflen the length in bytes of the data in the supplied buffer
43 * @param dup integer - the MQTT dup flag
44 * @param packetid integer - the MQTT packet identifier
45 * @param count - number of members in the topicFilters array
46 * @param topicFilters - array of topic filter names
47 * @return the length of the serialized data. <= 0 indicates error
48 */
MQTTSerialize_unsubscribe(unsigned char * buf,int buflen,unsigned char dup,unsigned short packetid,int count,MQTTString topicFilters[])49 int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
50 int count, MQTTString topicFilters[])
51 {
52 unsigned char *ptr = buf;
53 MQTTHeader header = {0};
54 int rem_len = 0;
55 int rc = -1;
56 int i = 0;
57
58 FUNC_ENTRY;
59 if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen)
60 {
61 rc = MQTTPACKET_BUFFER_TOO_SHORT;
62 goto exit;
63 }
64
65 header.byte = 0;
66 header.bits.type = UNSUBSCRIBE;
67 header.bits.dup = dup;
68 header.bits.qos = 1;
69 writeChar(&ptr, header.byte); /* write header */
70
71 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
72
73 writeInt(&ptr, packetid);
74
75 for (i = 0; i < count; ++i)
76 writeMQTTString(&ptr, topicFilters[i]);
77
78 rc = ptr - buf;
79 exit:
80 FUNC_EXIT_RC(rc);
81 return rc;
82 }
83
84
85 /**
86 * Deserializes the supplied (wire) buffer into unsuback data
87 * @param packetid returned integer - the MQTT packet identifier
88 * @param buf the raw buffer data, of the correct length determined by the remaining length field
89 * @param buflen the length in bytes of the data in the supplied buffer
90 * @return error code. 1 is success, 0 is failure
91 */
MQTTDeserialize_unsuback(unsigned short * packetid,unsigned char * buf,int buflen)92 int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen)
93 {
94 unsigned char type = 0;
95 unsigned char dup = 0;
96 int rc = 0;
97
98 FUNC_ENTRY;
99 rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
100 if (type == UNSUBACK)
101 rc = 1;
102 FUNC_EXIT_RC(rc);
103 return rc;
104 }
105
106
107