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 
20 #include <string.h>
21 
22 
23 /**
24  * Determines the length of the MQTT publish packet that would be produced using
25  * 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
30  * packet
31  */
MQTTSerialize_publishLength(int qos,MQTTString topicName,int payloadlen)32 int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
33 {
34     int len = 0;
35 
36     len += 2 + MQTTstrlen(topicName) + payloadlen;
37     if (qos > 0) {
38         len += 2; /* packetid */
39     }
40     return len;
41 }
42 
43 /**
44  * Serializes the supplied publish data into the supplied buffer, ready for
45  * sending
46  * @param buf the buffer into which the packet will be serialized
47  * @param buflen the length in bytes of the supplied buffer
48  * @param dup integer - the MQTT dup flag
49  * @param qos integer - the MQTT QoS value
50  * @param retained integer - the MQTT retained flag
51  * @param packetid integer - the MQTT packet identifier
52  * @param topicName MQTTString - the MQTT topic in the publish
53  * @param payload byte buffer - the MQTT publish payload
54  * @param payloadlen integer - the length of the MQTT payload
55  * @return the length of the serialized data.  <= 0 indicates error
56  */
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)57 int MQTTSerialize_publish(unsigned char *buf, int buflen, unsigned char dup,
58                           int qos, unsigned char retained,
59                           unsigned short packetid, MQTTString topicName,
60                           unsigned char *payload, int payloadlen)
61 {
62     unsigned char *ptr = buf;
63     MQTTHeader header = { 0 };
64     int rem_len = 0;
65     int rc = 0;
66 
67     rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen);
68     if (MQTTPacket_len(rem_len) > buflen) {
69         rc = MQTTPACKET_BUFFER_TOO_SHORT;
70         goto exit;
71     }
72 
73     MQTT_HEADER_SET_TYPE(header.byte, PUBLISH);
74     MQTT_HEADER_SET_DUP(header.byte, dup);
75     MQTT_HEADER_SET_QOS(header.byte, qos);
76     MQTT_HEADER_SET_RETAIN(header.byte, retained);
77     writeChar(&ptr, header.byte); /* write header */
78 
79     ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */
80     ;
81 
82     writeMQTTString(&ptr, topicName);
83 
84     if (qos > 0) {
85         writeInt(&ptr, packetid);
86     }
87 
88     memcpy(ptr, payload, payloadlen);
89     ptr += payloadlen;
90 
91     rc = ptr - buf;
92 
93 exit:
94     return rc;
95 }
96 
97 /**
98  * Serializes the ack packet into the supplied buffer.
99  * @param buf the buffer into which the packet will be serialized
100  * @param buflen the length in bytes of the supplied buffer
101  * @param type the MQTT packet type
102  * @param dup the MQTT dup flag
103  * @param packetid the MQTT packet identifier
104  * @return serialized length, or error if 0
105  */
MQTTSerialize_ack(unsigned char * buf,int buflen,unsigned char packettype,unsigned char dup,unsigned short packetid)106 int MQTTSerialize_ack(unsigned char *buf, int buflen, unsigned char packettype,
107                       unsigned char dup, unsigned short packetid)
108 {
109     MQTTHeader header = { 0 };
110     int rc = 0;
111     unsigned char *ptr = buf;
112 
113     if (buflen < 4) {
114         rc = MQTTPACKET_BUFFER_TOO_SHORT;
115         goto exit;
116     }
117     MQTT_HEADER_SET_TYPE(header.byte, packettype);
118     MQTT_HEADER_SET_DUP(header.byte, dup);
119     MQTT_HEADER_SET_QOS(header.byte, ((packettype == PUBREL) ? 1 : 0));
120     writeChar(&ptr, header.byte); /* write header */
121 
122     ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
123     writeInt(&ptr, packetid);
124     rc = ptr - buf;
125 exit:
126     return rc;
127 }
128 
129 /**
130  * Serializes a puback packet into the supplied buffer.
131  * @param buf the buffer into which the packet will be serialized
132  * @param buflen the length in bytes of the supplied buffer
133  * @param packetid integer - the MQTT packet identifier
134  * @return serialized length, or error if 0
135  */
136 /*  int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short
137  * packetid) */
138 /*  { */
139 /*   return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid); */
140 /*  } */
141 
142 #if WITH_MQTT_QOS2_PACKET
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,
152                          unsigned short packetid)
153 {
154     return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid);
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,
165                           unsigned short packetid)
166 {
167     return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid);
168 }
169 #endif /* #if WITH_MQTT_QOS2_PACKET */
170