• 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 /**
24   * Deserializes the supplied (wire) buffer into unsubscribe data
25   * @param dup integer returned - the MQTT dup flag
26   * @param packetid integer returned - the MQTT packet identifier
27   * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
28   * @param count - number of members in the topicFilters and requestedQoSs arrays
29   * @param topicFilters - array of topic filter names
30   * @param buf the raw buffer data, of the correct length determined by the remaining length field
31   * @param buflen the length in bytes of the data in the supplied buffer
32   * @return the length of the serialized data.  <= 0 indicates error
33   */
MQTTDeserialize_unsubscribe(unsigned char * dup,unsigned short * packetid,int maxcount,int * count,MQTTString topicFilters[],unsigned char * buf,int len)34 int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount,
35     int* count, MQTTString topicFilters[],
36     unsigned char* buf, int len)
37 {
38     MQTTHeader header = {0};
39     unsigned char* curdata = buf;
40     unsigned char* enddata = NULL;
41     int rc = 0;
42     int mylen = 0;
43 
44     FUNC_ENTRY;
45     header.byte = readChar(&curdata);
46     if (header.bits.type != UNSUBSCRIBE)
47         FUNC_EXIT_RC(rc);
48         return rc;
49     *dup = header.bits.dup;
50 
51     curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
52     enddata = curdata + mylen;
53 
54     *packetid = readInt(&curdata);
55 
56     *count = 0;
57     while (curdata < enddata) {
58         if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
59             FUNC_EXIT_RC(rc);
60             return rc;
61         (*count)++;
62     }
63 
64     rc = 1;
65     return rc;
66 }
67 
68 
69 /**
70   * Serializes the supplied unsuback data into the supplied buffer, ready for sending
71   * @param buf the buffer into which the packet will be serialized
72   * @param buflen the length in bytes of the supplied buffer
73   * @param packetid integer - the MQTT packet identifier
74   * @return the length of the serialized data.  <= 0 indicates error
75   */
MQTTSerialize_unsuback(unsigned char * buf,int buflen,unsigned short packetid)76 int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid)
77 {
78     MQTTHeader header = {0};
79     int rc = 0;
80     unsigned char *ptr = buf;
81 
82     FUNC_ENTRY;
83     if (buflen < 2) {
84         rc = MQTTPACKET_BUFFER_TOO_SHORT;
85         FUNC_EXIT_RC(rc);
86         return rc;
87     }
88     header.byte = 0;
89     header.bits.type = UNSUBACK;
90     writeChar(&ptr, header.byte); /* write header */
91 
92     ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
93 
94     writeInt(&ptr, packetid);
95 
96     rc = ptr - buf;
97     return rc;
98 }
99