Junichi Katsu / Mbed OS mbed-os-example-milkcocoa

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

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 
00062     FUNC_ENTRY;
00063     if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
00064     {
00065         rc = MQTTPACKET_BUFFER_TOO_SHORT;
00066         goto exit;
00067     }
00068 
00069     header.bits.type = PUBLISH;
00070     header.bits.dup = dup;
00071     header.bits.qos = qos;
00072     header.bits.retain = retained;
00073     writeChar(&ptr, header.byte); /* write header */
00074 
00075     ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
00076 
00077     writeMQTTString(&ptr, topicName);
00078 
00079     if (qos > 0)
00080         writeInt(&ptr, packetid);
00081 
00082     memcpy(ptr, payload, payloadlen);
00083     ptr += payloadlen;
00084 
00085     rc = ptr - buf;
00086 
00087 exit:
00088     FUNC_EXIT_RC(rc);
00089     return rc;
00090 }
00091 
00092 
00093 
00094 /**
00095   * Serializes the ack packet into the supplied buffer.
00096   * @param buf the buffer into which the packet will be serialized
00097   * @param buflen the length in bytes of the supplied buffer
00098   * @param type the MQTT packet type
00099   * @param dup the MQTT dup flag
00100   * @param packetid the MQTT packet identifier
00101   * @return serialized length, or error if 0
00102   */
00103 int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
00104 {
00105     MQTTHeader header = {0};
00106     int rc = 0;
00107     unsigned char *ptr = buf;
00108 
00109     FUNC_ENTRY;
00110     if (buflen < 4)
00111     {
00112         rc = MQTTPACKET_BUFFER_TOO_SHORT;
00113         goto exit;
00114     }
00115     header.bits.type = packettype;
00116     header.bits.dup = dup;
00117     header.bits.qos = 0;
00118     writeChar(&ptr, header.byte); /* write header */
00119 
00120     ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
00121     writeInt(&ptr, packetid);
00122     rc = ptr - buf;
00123 exit:
00124     FUNC_EXIT_RC(rc);
00125     return rc;
00126 }
00127 
00128 
00129 /**
00130   * Serializes a puback packet into the supplied buffer.
00131   * @param buf the buffer into which the packet will be serialized
00132   * @param buflen the length in bytes of the supplied buffer
00133   * @param packetid integer - the MQTT packet identifier
00134   * @return serialized length, or error if 0
00135   */
00136 int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid)
00137 {
00138     return MQTTSerialize_ack(buf, buflen, PUBACK, packetid, 0);
00139 }
00140 
00141 
00142 /**
00143   * Serializes a pubrel packet into the supplied buffer.
00144   * @param buf the buffer into which the packet will be serialized
00145   * @param buflen the length in bytes of the supplied buffer
00146   * @param dup integer - the MQTT dup flag
00147   * @param packetid integer - the MQTT packet identifier
00148   * @return serialized length, or error if 0
00149   */
00150 int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid)
00151 {
00152     return MQTTSerialize_ack(buf, buflen, PUBREL, packetid, dup);
00153 }
00154 
00155 
00156 /**
00157   * Serializes a pubrel packet into the supplied buffer.
00158   * @param buf the buffer into which the packet will be serialized
00159   * @param buflen the length in bytes of the supplied buffer
00160   * @param packetid integer - the MQTT packet identifier
00161   * @return serialized length, or error if 0
00162   */
00163 int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid)
00164 {
00165     return MQTTSerialize_ack(buf, buflen, PUBCOMP, packetid, 0);
00166 }
00167 
00168