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 * Ian Craggs - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=453144
16 *******************************************************************************/
17
18 #include "MQTTPacket.h"
19 #include "StackTrace.h"
20
21 #include <string.h>
22
23
24 /**
25 * Determines the length of the MQTT publish packet that would be produced using the supplied parameters
26 * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
27 * @param topicName the topic name to be used in the publish
28 * @param payloadlen the length of the payload to be sent
29 * @return the length of buffer needed to contain the serialized version of the packet
30 */
MQTTSerialize_publishLength(int qos,MQTTString topicName,int payloadlen)31 int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
32 {
33 int len = 0;
34
35 len += 2 + MQTTstrlen(topicName) + payloadlen;
36 if (qos > 0)
37 len += 2; /* packetid */
38 return len;
39 }
40
41
42 /**
43 * Serializes the supplied publish data into the supplied buffer, ready for sending
44 * @param buf the buffer into which the packet will be serialized
45 * @param buflen the length in bytes of the supplied buffer
46 * @param dup integer - the MQTT dup flag
47 * @param qos integer - the MQTT QoS value
48 * @param retained integer - the MQTT retained flag
49 * @param packetid integer - the MQTT packet identifier
50 * @param topicName MQTTString - the MQTT topic in the publish
51 * @param payload byte buffer - the MQTT publish payload
52 * @param payloadlen integer - the length of the MQTT payload
53 * @return the length of the serialized data. <= 0 indicates error
54 */
MQTTSerialize_publish(unsigned char * buf,int buflen,unsigned char dup,int qos,unsigned char retained,unsigned short packetid,MQTTString topicName,unsigned char * payload,int payloadlen)55 int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
56 MQTTString topicName, unsigned char* payload, int payloadlen)
57 {
58 unsigned char *ptr = buf;
59 MQTTHeader header = {0};
60 int rem_len = 0;
61 int rc = 0;
62
63 FUNC_ENTRY;
64 if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
65 {
66 rc = MQTTPACKET_BUFFER_TOO_SHORT;
67 goto exit;
68 }
69
70 header.bits.type = PUBLISH;
71 header.bits.dup = dup;
72 header.bits.qos = qos;
73 header.bits.retain = retained;
74 writeChar(&ptr, header.byte); /* write header */
75
76 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
77
78 writeMQTTString(&ptr, topicName);
79
80 if (qos > 0)
81 writeInt(&ptr, packetid);
82
83 memcpy(ptr, payload, payloadlen);
84 ptr += payloadlen;
85
86 rc = ptr - buf;
87
88 exit:
89 FUNC_EXIT_RC(rc);
90 return rc;
91 }
92
93
94
95 /**
96 * Serializes the ack packet into the supplied buffer.
97 * @param buf the buffer into which the packet will be serialized
98 * @param buflen the length in bytes of the supplied buffer
99 * @param type the MQTT packet type
100 * @param dup the MQTT dup flag
101 * @param packetid the MQTT packet identifier
102 * @return serialized length, or error if 0
103 */
MQTTSerialize_ack(unsigned char * buf,int buflen,unsigned char packettype,unsigned char dup,unsigned short packetid)104 int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
105 {
106 MQTTHeader header = {0};
107 int rc = 0;
108 unsigned char *ptr = buf;
109
110 FUNC_ENTRY;
111 if (buflen < 4)
112 {
113 rc = MQTTPACKET_BUFFER_TOO_SHORT;
114 goto exit;
115 }
116 header.bits.type = packettype;
117 header.bits.dup = dup;
118 header.bits.qos = (packettype == PUBREL) ? 1 : 0;
119 writeChar(&ptr, header.byte); /* write header */
120
121 ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
122 writeInt(&ptr, packetid);
123 rc = ptr - buf;
124 exit:
125 FUNC_EXIT_RC(rc);
126 return rc;
127 }
128
129
130 /**
131 * Serializes a puback packet into the supplied buffer.
132 * @param buf the buffer into which the packet will be serialized
133 * @param buflen the length in bytes of the supplied buffer
134 * @param packetid integer - the MQTT packet identifier
135 * @return serialized length, or error if 0
136 */
MQTTSerialize_puback(unsigned char * buf,int buflen,unsigned short packetid)137 int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid)
138 {
139 return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid);
140 }
141
142
143 /**
144 * Serializes a pubrel packet into the supplied buffer.
145 * @param buf the buffer into which the packet will be serialized
146 * @param buflen the length in bytes of the supplied buffer
147 * @param dup integer - the MQTT dup flag
148 * @param packetid integer - the MQTT packet identifier
149 * @return serialized length, or error if 0
150 */
MQTTSerialize_pubrel(unsigned char * buf,int buflen,unsigned char dup,unsigned short packetid)151 int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid)
152 {
153 return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid);
154 }
155
156
157 /**
158 * Serializes a pubrel packet into the supplied buffer.
159 * @param buf the buffer into which the packet will be serialized
160 * @param buflen the length in bytes of the supplied buffer
161 * @param packetid integer - the MQTT packet identifier
162 * @return serialized length, or error if 0
163 */
MQTTSerialize_pubcomp(unsigned char * buf,int buflen,unsigned short packetid)164 int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid)
165 {
166 return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid);
167 }
168
169
170