Basic C library for MQTT packet serialization and deserialization

Dependents:   MQTT MQTT MQTT MQTT ... more

Fork of MQTTPacket by MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTSerializePublish.c Source File

MQTTSerializePublish.c

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *    Ian Craggs - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=453144
00016  *******************************************************************************/
00017 
00018 #include "MQTTPacket.h"
00019 #include "StackTrace.h"
00020 
00021 #include <string.h>
00022 
00023 
00024 /**
00025   * Determines the length of the MQTT publish packet that would be produced using the supplied parameters
00026   * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
00027   * @param topicName the topic name to be used in the publish  
00028   * @param payloadlen the length of the payload to be sent
00029   * @return the length of buffer needed to contain the serialized version of the packet
00030   */
00031 int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
00032 {
00033     int len = 0;
00034 
00035     len += 2 + MQTTstrlen(topicName) + payloadlen;
00036     if (qos > 0)
00037         len += 2; /* packetid */
00038     return len;
00039 }
00040 
00041 
00042 /**
00043   * Serializes the supplied publish data into the supplied buffer, ready for sending
00044   * @param buf the buffer into which the packet will be serialized
00045   * @param buflen the length in bytes of the supplied buffer
00046   * @param dup integer - the MQTT dup flag
00047   * @param qos integer - the MQTT QoS value
00048   * @param retained integer - the MQTT retained flag
00049   * @param packetid integer - the MQTT packet identifier
00050   * @param topicName MQTTString - the MQTT topic in the publish
00051   * @param payload byte buffer - the MQTT publish payload
00052   * @param payloadlen integer - the length of the MQTT payload
00053   * @return the length of the serialized data.  <= 0 indicates error
00054   */
00055 int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
00056         MQTTString topicName, unsigned char* payload, int payloadlen)
00057 {
00058     unsigned char *ptr = buf;
00059     MQTTHeader header = {0};
00060     int rem_len = 0;
00061     int rc = 0;
00062 
00063     FUNC_ENTRY;
00064     if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
00065     {
00066         rc = MQTTPACKET_BUFFER_TOO_SHORT;
00067         goto exit;
00068     }
00069 
00070     header.bits.type = PUBLISH;
00071     header.bits.dup = dup;
00072     header.bits.qos = qos;
00073     header.bits.retain = retained;
00074     writeChar(&ptr, header.byte); /* write header */
00075 
00076     ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
00077 
00078     writeMQTTString(&ptr, topicName);
00079 
00080     if (qos > 0)
00081         writeInt(&ptr, packetid);
00082 
00083     memcpy(ptr, payload, payloadlen);
00084     ptr += payloadlen;
00085 
00086     rc = ptr - buf;
00087 
00088 exit:
00089     FUNC_EXIT_RC(rc);
00090     return rc;
00091 }
00092 
00093 
00094 
00095 /**
00096   * Serializes the ack packet into the supplied buffer.
00097   * @param buf the buffer into which the packet will be serialized
00098   * @param buflen the length in bytes of the supplied buffer
00099   * @param type the MQTT packet type
00100   * @param dup the MQTT dup flag
00101   * @param packetid the MQTT packet identifier
00102   * @return serialized length, or error if 0
00103   */
00104 int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
00105 {
00106     MQTTHeader header = {0};
00107     int rc = 0;
00108     unsigned char *ptr = buf;
00109 
00110     FUNC_ENTRY;
00111     if (buflen < 4)
00112     {
00113         rc = MQTTPACKET_BUFFER_TOO_SHORT;
00114         goto exit;
00115     }
00116     header.bits.type = packettype;
00117     header.bits.dup = dup;
00118     header.bits.qos = (packettype == PUBREL) ? 1 : 0;
00119     writeChar(&ptr, header.byte); /* write header */
00120 
00121     ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
00122     writeInt(&ptr, packetid);
00123     rc = ptr - buf;
00124 exit:
00125     FUNC_EXIT_RC(rc);
00126     return rc;
00127 }
00128 
00129 
00130 /**
00131   * Serializes a puback packet into the supplied buffer.
00132   * @param buf the buffer into which the packet will be serialized
00133   * @param buflen the length in bytes of the supplied buffer
00134   * @param packetid integer - the MQTT packet identifier
00135   * @return serialized length, or error if 0
00136   */
00137 int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid)
00138 {
00139     return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid);
00140 }
00141 
00142 
00143 /**
00144   * Serializes a pubrel packet into the supplied buffer.
00145   * @param buf the buffer into which the packet will be serialized
00146   * @param buflen the length in bytes of the supplied buffer
00147   * @param dup integer - the MQTT dup flag
00148   * @param packetid integer - the MQTT packet identifier
00149   * @return serialized length, or error if 0
00150   */
00151 int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid)
00152 {
00153     return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid);
00154 }
00155 
00156 
00157 /**
00158   * Serializes a pubrel packet into the supplied buffer.
00159   * @param buf the buffer into which the packet will be serialized
00160   * @param buflen the length in bytes of the supplied buffer
00161   * @param packetid integer - the MQTT packet identifier
00162   * @return serialized length, or error if 0
00163   */
00164 int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid)
00165 {
00166     return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid);
00167 }
00168