V.06 11/3

Dependencies:   FT6206 SDFileSystem SPI_TFT_ILI9341 TFT_fonts

Fork of ATT_AWS_IoT_demo by attiot

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTSerializePublish.cpp Source File

MQTTSerializePublish.cpp

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 size_t MQTTSerialize_GetPublishLength(uint8_t qos, MQTTString topicName, size_t payloadlen) {
00032     size_t len = 0;
00033 
00034     len += 2 + MQTTstrlen(topicName) + payloadlen;
00035     if(qos > 0) {
00036         len += 2; /* packetid */
00037     }
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 MQTTReturnCode MQTTSerialize_publish(unsigned char *buf, size_t buflen, uint8_t dup,
00056                           QoS qos, uint8_t retained, uint16_t packetid,
00057                           MQTTString topicName, unsigned char *payload, size_t payloadlen,
00058                           uint32_t *serialized_len) {
00059         unsigned char *ptr = buf;
00060         MQTTHeader header = {0};
00061         size_t rem_len = 0;
00062         MQTTReturnCode rc = MQTTPacket_InitHeader(&header, PUBLISH, qos, dup, retained);
00063 
00064     FUNC_ENTRY;
00065     if(NULL == buf || NULL == payload || NULL == serialized_len) {
00066         FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
00067         return MQTT_NULL_VALUE_ERROR;
00068     }
00069 
00070     rem_len = MQTTSerialize_GetPublishLength(qos, topicName, payloadlen);
00071     if(MQTTPacket_len(rem_len) > buflen) {
00072         FUNC_EXIT_RC(MQTTPACKET_BUFFER_TOO_SHORT);
00073         return MQTTPACKET_BUFFER_TOO_SHORT;
00074     }
00075 
00076     if(SUCCESS != rc) {
00077         FUNC_EXIT_RC(rc);
00078         return rc;
00079     }
00080     writeChar(&ptr, header.byte); /* write header */
00081 
00082     ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
00083 
00084     writeMQTTString(&ptr, topicName);
00085 
00086     if(qos > 0) {
00087         writeInt(&ptr, packetid);
00088     }
00089 
00090     memcpy(ptr, payload, payloadlen);
00091     ptr += payloadlen;
00092 
00093     *serialized_len = (uint32_t)(ptr - buf);
00094 
00095     FUNC_EXIT_RC(SUCCESS);
00096     return SUCCESS;
00097 }
00098 
00099 /**
00100   * Serializes the ack packet into the supplied buffer.
00101   * @param buf the buffer into which the packet will be serialized
00102   * @param buflen the length in bytes of the supplied buffer
00103   * @param type the MQTT packet type
00104   * @param dup the MQTT dup flag
00105   * @param packetid the MQTT packet identifier
00106   * @return serialized length, or error if 0
00107   */
00108 MQTTReturnCode MQTTSerialize_ack(unsigned char *buf, size_t buflen,
00109                       unsigned char type, uint8_t dup, uint16_t packetid,
00110                       uint32_t *serialized_len) {
00111         MQTTHeader header = {0};
00112         unsigned char *ptr = buf;
00113         QoS requestQoS = (PUBREL == type) ? QOS1 : QOS0;
00114         MQTTReturnCode rc = MQTTPacket_InitHeader(&header, (MessageTypes)type, requestQoS, dup, 0);
00115 
00116     FUNC_ENTRY;
00117     if(NULL == buf || serialized_len == NULL) {
00118         FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR);
00119         return MQTT_NULL_VALUE_ERROR;
00120     }
00121 
00122     /* Minimum byte length required by ACK headers is
00123      * 2 for fixed and 2 for variable part */
00124     if(4 > buflen) {
00125         FUNC_EXIT_RC(MQTTPACKET_BUFFER_TOO_SHORT);
00126         return MQTTPACKET_BUFFER_TOO_SHORT;
00127     }
00128 
00129     if(SUCCESS != rc) {
00130         FUNC_EXIT_RC(rc);
00131         return rc;
00132     }
00133     writeChar(&ptr, header.byte); /* write header */
00134 
00135     ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
00136     writePacketId(&ptr, packetid);
00137     *serialized_len = (uint32_t)(ptr - buf);
00138 
00139     FUNC_EXIT_RC(SUCCESS);
00140     return SUCCESS;
00141 }
00142 
00143 
00144 /**
00145   * Serializes a puback packet into the supplied buffer.
00146   * @param buf the buffer into which the packet will be serialized
00147   * @param buflen the length in bytes of the supplied buffer
00148   * @param packetid integer - the MQTT packet identifier
00149   * @return serialized length, or error if 0
00150   */
00151 MQTTReturnCode MQTTSerialize_puback(unsigned char* buf, size_t buflen,
00152                                     uint16_t packetid, uint32_t *serialized_len) {
00153     return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid, serialized_len);
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 dup integer - the MQTT dup flag
00162   * @param packetid integer - the MQTT packet identifier
00163   * @return serialized length, or error if 0
00164   */
00165 MQTTReturnCode MQTTSerialize_pubrel(unsigned char *buf, size_t buflen,
00166                                     unsigned char dup, uint16_t packetid,
00167                                     uint32_t *serialized_len) {
00168     return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid, serialized_len);
00169 }
00170 
00171 
00172 /**
00173   * Serializes a pubrel packet into the supplied buffer.
00174   * @param buf the buffer into which the packet will be serialized
00175   * @param buflen the length in bytes of the supplied buffer
00176   * @param packetid integer - the MQTT packet identifier
00177   * @return serialized length, or error if 0
00178   */
00179 MQTTReturnCode MQTTSerialize_pubcomp(unsigned char *buf, size_t buflen,
00180                                      uint16_t packetid, uint32_t *serialized_len) {
00181     return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid, serialized_len);
00182 }
00183 
00184 
00185 
00186