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, int* count, MQTTString topicFilters[],
35 unsigned char* buf, int len)
36 {
37 MQTTHeader header = {0};
38 unsigned char* curdata = buf;
39 unsigned char* enddata = NULL;
40 int rc = 0;
41 int mylen = 0;
42
43 FUNC_ENTRY;
44 header.byte = readChar(&curdata);
45 if (header.bits.type != UNSUBSCRIBE)
46 goto exit;
47 *dup = header.bits.dup;
48
49 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
50 enddata = curdata + mylen;
51
52 *packetid = readInt(&curdata);
53
54 *count = 0;
55 while (curdata < enddata)
56 {
57 if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
58 goto exit;
59 (*count)++;
60 }
61
62 rc = 1;
63 exit:
64 FUNC_EXIT_RC(rc);
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 {
85 rc = MQTTPACKET_BUFFER_TOO_SHORT;
86 goto exit;
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 exit:
98 FUNC_EXIT_RC(rc);
99 return rc;
100 }
101
102
103