source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/libraries/Temboo/src/utility/MQTTSerializePublish.c @ 46

Last change on this file since 46 was 46, checked in by jrpelegrina, 4 years ago

First release to Xenial

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