eric liang / MQTTPacket

Dependents:   MQTT

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  *******************************************************************************/
00016 
00017 #include "MQTTPacket.h"
00018 #include "StackTrace.h"
00019 
00020 #include <string.h>
00021 
00022 
00023 /**
00024   * Determines the length of the MQTT publish packet that would be produced using the supplied parameters
00025   * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
00026   * @param topicName the topic name to be used in the publish  
00027   * @param payloadlen the length of the payload to be sent
00028   * @return the length of buffer needed to contain the serialized version of the packet
00029   */
00030 int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
00031 {
00032     int len = 0;
00033 
00034     len += 2 + MQTTstrlen(topicName) + payloadlen;
00035     if (qos > 0)
00036         len += 2; /* packetid */
00037     return len;
00038 }
00039 
00040 
00041 /**
00042   * Serializes the supplied publish data into the supplied buffer, ready for sending
00043   * @param buf the buffer into which the packet will be serialized
00044   * @param buflen the length in bytes of the supplied buffer
00045   * @param dup integer - the MQTT dup flag
00046   * @param qos integer - the MQTT QoS value
00047   * @param retained integer - the MQTT retained flag
00048   * @param packetid integer - the MQTT packet identifier
00049   * @param topicName MQTTString - the MQTT topic in the publish
00050   * @param payload byte buffer - the MQTT publish payload
00051   * @param payloadlen integer - the length of the MQTT payload
00052   * @return the length of the serialized data.  <= 0 indicates error
00053   */
00054 int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
00055         MQTTString topicName, unsigned char* payload, int payloadlen)
00056 {
00057     unsigned char *ptr = buf;
00058     MQTTHeader header = {0};
00059     int rem_len = 0;
00060     int rc = 0;
00061     printf("eric serial 1\n");
00062     FUNC_ENTRY;
00063     if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
00064     {
00065         printf("eric serial error payload=%d rem %d buflen=%d\n",payloadlen, rem_len, buflen);
00066         rc = MQTTPACKET_BUFFER_TOO_SHORT;
00067         goto exit;
00068     }
00069     printf("eric serial 2\n");
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 = 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, packetid, 0);
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, packetid, dup);
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, packetid, 0);
00167 }
00168 
00169