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