• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         rc = MQTTPACKET_BUFFER_TOO_SHORT;
61         FUNC_EXIT_RC(rc);
62         return rc;
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     return rc;
80 }
81 /**
82   * Deserializes the supplied (wire) buffer into unsuback data
83   * @param packetid returned integer - the MQTT packet identifier
84   * @param buf the raw buffer data, of the correct length determined by the remaining length field
85   * @param buflen the length in bytes of the data in the supplied buffer
86   * @return error code.  1 is success, 0 is failure
87   */
MQTTDeserialize_unsuback(unsigned short * packetid,unsigned char * buf,int buflen)88 int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen)
89 {
90     unsigned char type = 0;
91     unsigned char dup = 0;
92     int rc = 0;
93 
94     FUNC_ENTRY;
95     rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
96     if (type == UNSUBACK)
97         rc = 1;
98     FUNC_EXIT_RC(rc);
99     return rc;
100 }
101