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 subscribe 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 requestedQoSs - array of requested QoS
31   * @param buf the raw buffer data, of the correct length determined by the remaining length field
32   * @param buflen the length in bytes of the data in the supplied buffer
33   * @return the length of the serialized data.  <= 0 indicates error
34   */
MQTTDeserialize_subscribe(unsigned char * dup,unsigned short * packetid,int maxcount,int * count,MQTTString topicFilters[],int requestedQoSs[],unsigned char * buf,int buflen)35 int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
36 	int requestedQoSs[], unsigned char* buf, int buflen)
37 {
38 	MQTTHeader header = {0};
39 	unsigned char* curdata = buf;
40 	unsigned char* enddata = NULL;
41 	int rc = -1;
42 	int mylen = 0;
43 
44 	FUNC_ENTRY;
45 	header.byte = readChar(&curdata);
46 	if (header.bits.type != SUBSCRIBE)
47 		goto exit;
48 	*dup = header.bits.dup;
49 
50 	curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
51 	enddata = curdata + mylen;
52 
53 	*packetid = readInt(&curdata);
54 
55 	*count = 0;
56 	while (curdata < enddata)
57 	{
58 		if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
59 			goto exit;
60 		if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */
61 			goto exit;
62 		requestedQoSs[*count] = readChar(&curdata);
63 		(*count)++;
64 	}
65 
66 	rc = 1;
67 exit:
68 	FUNC_EXIT_RC(rc);
69 	return rc;
70 }
71 
72 
73 /**
74   * Serializes the supplied suback data into the supplied buffer, ready for sending
75   * @param buf the buffer into which the packet will be serialized
76   * @param buflen the length in bytes of the supplied buffer
77   * @param packetid integer - the MQTT packet identifier
78   * @param count - number of members in the grantedQoSs array
79   * @param grantedQoSs - array of granted QoS
80   * @return the length of the serialized data.  <= 0 indicates error
81   */
MQTTSerialize_suback(unsigned char * buf,int buflen,unsigned short packetid,int count,int * grantedQoSs)82 int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs)
83 {
84 	MQTTHeader header = {0};
85 	int rc = -1;
86 	unsigned char *ptr = buf;
87 	int i;
88 
89 	FUNC_ENTRY;
90 	if (buflen < 2 + count)
91 	{
92 		rc = MQTTPACKET_BUFFER_TOO_SHORT;
93 		goto exit;
94 	}
95 	header.byte = 0;
96 	header.bits.type = SUBACK;
97 	writeChar(&ptr, header.byte); /* write header */
98 
99 	ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */
100 
101 	writeInt(&ptr, packetid);
102 
103 	for (i = 0; i < count; ++i)
104 		writeChar(&ptr, grantedQoSs[i]);
105 
106 	rc = ptr - buf;
107 exit:
108 	FUNC_EXIT_RC(rc);
109 	return rc;
110 }
111 
112 
113