Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vpcola 0:f1d3878b8dd9 1 /*******************************************************************************
vpcola 0:f1d3878b8dd9 2 * Copyright (c) 2014 IBM Corp.
vpcola 0:f1d3878b8dd9 3 *
vpcola 0:f1d3878b8dd9 4 * All rights reserved. This program and the accompanying materials
vpcola 0:f1d3878b8dd9 5 * are made available under the terms of the Eclipse Public License v1.0
vpcola 0:f1d3878b8dd9 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
vpcola 0:f1d3878b8dd9 7 *
vpcola 0:f1d3878b8dd9 8 * The Eclipse Public License is available at
vpcola 0:f1d3878b8dd9 9 * http://www.eclipse.org/legal/epl-v10.html
vpcola 0:f1d3878b8dd9 10 * and the Eclipse Distribution License is available at
vpcola 0:f1d3878b8dd9 11 * http://www.eclipse.org/org/documents/edl-v10.php.
vpcola 0:f1d3878b8dd9 12 *
vpcola 0:f1d3878b8dd9 13 * Contributors:
vpcola 0:f1d3878b8dd9 14 * Ian Craggs - initial API and implementation and/or initial documentation
vpcola 0:f1d3878b8dd9 15 *******************************************************************************/
vpcola 0:f1d3878b8dd9 16
vpcola 0:f1d3878b8dd9 17 #include "MQTTPacket.h"
vpcola 0:f1d3878b8dd9 18 #include "StackTrace.h"
vpcola 0:f1d3878b8dd9 19
vpcola 0:f1d3878b8dd9 20 #include <string.h>
vpcola 0:f1d3878b8dd9 21
vpcola 0:f1d3878b8dd9 22
vpcola 0:f1d3878b8dd9 23 /**
vpcola 0:f1d3878b8dd9 24 * Determines the length of the MQTT publish packet that would be produced using the supplied parameters
vpcola 0:f1d3878b8dd9 25 * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
vpcola 0:f1d3878b8dd9 26 * @param topicName the topic name to be used in the publish
vpcola 0:f1d3878b8dd9 27 * @param payloadlen the length of the payload to be sent
vpcola 0:f1d3878b8dd9 28 * @return the length of buffer needed to contain the serialized version of the packet
vpcola 0:f1d3878b8dd9 29 */
vpcola 0:f1d3878b8dd9 30 int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
vpcola 0:f1d3878b8dd9 31 {
vpcola 0:f1d3878b8dd9 32 int len = 0;
vpcola 0:f1d3878b8dd9 33
vpcola 0:f1d3878b8dd9 34 len += 2 + MQTTstrlen(topicName) + payloadlen;
vpcola 0:f1d3878b8dd9 35 if (qos > 0)
vpcola 0:f1d3878b8dd9 36 len += 2; /* packetid */
vpcola 0:f1d3878b8dd9 37 return len;
vpcola 0:f1d3878b8dd9 38 }
vpcola 0:f1d3878b8dd9 39
vpcola 0:f1d3878b8dd9 40
vpcola 0:f1d3878b8dd9 41 /**
vpcola 0:f1d3878b8dd9 42 * Serializes the supplied publish data into the supplied buffer, ready for sending
vpcola 0:f1d3878b8dd9 43 * @param buf the buffer into which the packet will be serialized
vpcola 0:f1d3878b8dd9 44 * @param buflen the length in bytes of the supplied buffer
vpcola 0:f1d3878b8dd9 45 * @param dup integer - the MQTT dup flag
vpcola 0:f1d3878b8dd9 46 * @param qos integer - the MQTT QoS value
vpcola 0:f1d3878b8dd9 47 * @param retained integer - the MQTT retained flag
vpcola 0:f1d3878b8dd9 48 * @param packetid integer - the MQTT packet identifier
vpcola 0:f1d3878b8dd9 49 * @param topicName MQTTString - the MQTT topic in the publish
vpcola 0:f1d3878b8dd9 50 * @param payload byte buffer - the MQTT publish payload
vpcola 0:f1d3878b8dd9 51 * @param payloadlen integer - the length of the MQTT payload
vpcola 0:f1d3878b8dd9 52 * @return the length of the serialized data. <= 0 indicates error
vpcola 0:f1d3878b8dd9 53 */
vpcola 0:f1d3878b8dd9 54 int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
vpcola 0:f1d3878b8dd9 55 MQTTString topicName, unsigned char* payload, int payloadlen)
vpcola 0:f1d3878b8dd9 56 {
vpcola 0:f1d3878b8dd9 57 unsigned char *ptr = buf;
vpcola 0:f1d3878b8dd9 58 MQTTHeader header = {0};
vpcola 0:f1d3878b8dd9 59 int rem_len = 0;
vpcola 0:f1d3878b8dd9 60 int rc = 0;
vpcola 0:f1d3878b8dd9 61
vpcola 0:f1d3878b8dd9 62 FUNC_ENTRY;
vpcola 0:f1d3878b8dd9 63 if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
vpcola 0:f1d3878b8dd9 64 {
vpcola 0:f1d3878b8dd9 65 rc = MQTTPACKET_BUFFER_TOO_SHORT;
vpcola 0:f1d3878b8dd9 66 goto exit;
vpcola 0:f1d3878b8dd9 67 }
vpcola 0:f1d3878b8dd9 68
vpcola 0:f1d3878b8dd9 69 header.bits.type = PUBLISH;
vpcola 0:f1d3878b8dd9 70 header.bits.dup = dup;
vpcola 0:f1d3878b8dd9 71 header.bits.qos = qos;
vpcola 0:f1d3878b8dd9 72 header.bits.retain = retained;
vpcola 0:f1d3878b8dd9 73 writeChar(&ptr, header.byte); /* write header */
vpcola 0:f1d3878b8dd9 74
vpcola 0:f1d3878b8dd9 75 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
vpcola 0:f1d3878b8dd9 76
vpcola 0:f1d3878b8dd9 77 writeMQTTString(&ptr, topicName);
vpcola 0:f1d3878b8dd9 78
vpcola 0:f1d3878b8dd9 79 if (qos > 0)
vpcola 0:f1d3878b8dd9 80 writeInt(&ptr, packetid);
vpcola 0:f1d3878b8dd9 81
vpcola 0:f1d3878b8dd9 82 memcpy(ptr, payload, payloadlen);
vpcola 0:f1d3878b8dd9 83 ptr += payloadlen;
vpcola 0:f1d3878b8dd9 84
vpcola 0:f1d3878b8dd9 85 rc = ptr - buf;
vpcola 0:f1d3878b8dd9 86
vpcola 0:f1d3878b8dd9 87 exit:
vpcola 0:f1d3878b8dd9 88 FUNC_EXIT_RC(rc);
vpcola 0:f1d3878b8dd9 89 return rc;
vpcola 0:f1d3878b8dd9 90 }
vpcola 0:f1d3878b8dd9 91
vpcola 0:f1d3878b8dd9 92
vpcola 0:f1d3878b8dd9 93
vpcola 0:f1d3878b8dd9 94 /**
vpcola 0:f1d3878b8dd9 95 * Serializes the ack packet into the supplied buffer.
vpcola 0:f1d3878b8dd9 96 * @param buf the buffer into which the packet will be serialized
vpcola 0:f1d3878b8dd9 97 * @param buflen the length in bytes of the supplied buffer
vpcola 0:f1d3878b8dd9 98 * @param type the MQTT packet type
vpcola 0:f1d3878b8dd9 99 * @param dup the MQTT dup flag
vpcola 0:f1d3878b8dd9 100 * @param packetid the MQTT packet identifier
vpcola 0:f1d3878b8dd9 101 * @return serialized length, or error if 0
vpcola 0:f1d3878b8dd9 102 */
vpcola 0:f1d3878b8dd9 103 int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
vpcola 0:f1d3878b8dd9 104 {
vpcola 0:f1d3878b8dd9 105 MQTTHeader header = {0};
vpcola 0:f1d3878b8dd9 106 int rc = 0;
vpcola 0:f1d3878b8dd9 107 unsigned char *ptr = buf;
vpcola 0:f1d3878b8dd9 108
vpcola 0:f1d3878b8dd9 109 FUNC_ENTRY;
vpcola 0:f1d3878b8dd9 110 if (buflen < 4)
vpcola 0:f1d3878b8dd9 111 {
vpcola 0:f1d3878b8dd9 112 rc = MQTTPACKET_BUFFER_TOO_SHORT;
vpcola 0:f1d3878b8dd9 113 goto exit;
vpcola 0:f1d3878b8dd9 114 }
vpcola 0:f1d3878b8dd9 115 header.bits.type = packettype;
vpcola 0:f1d3878b8dd9 116 header.bits.dup = dup;
vpcola 0:f1d3878b8dd9 117 header.bits.qos = 0;
vpcola 0:f1d3878b8dd9 118 writeChar(&ptr, header.byte); /* write header */
vpcola 0:f1d3878b8dd9 119
vpcola 0:f1d3878b8dd9 120 ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
vpcola 0:f1d3878b8dd9 121 writeInt(&ptr, packetid);
vpcola 0:f1d3878b8dd9 122 rc = ptr - buf;
vpcola 0:f1d3878b8dd9 123 exit:
vpcola 0:f1d3878b8dd9 124 FUNC_EXIT_RC(rc);
vpcola 0:f1d3878b8dd9 125 return rc;
vpcola 0:f1d3878b8dd9 126 }
vpcola 0:f1d3878b8dd9 127
vpcola 0:f1d3878b8dd9 128
vpcola 0:f1d3878b8dd9 129 /**
vpcola 0:f1d3878b8dd9 130 * Serializes a puback packet into the supplied buffer.
vpcola 0:f1d3878b8dd9 131 * @param buf the buffer into which the packet will be serialized
vpcola 0:f1d3878b8dd9 132 * @param buflen the length in bytes of the supplied buffer
vpcola 0:f1d3878b8dd9 133 * @param packetid integer - the MQTT packet identifier
vpcola 0:f1d3878b8dd9 134 * @return serialized length, or error if 0
vpcola 0:f1d3878b8dd9 135 */
vpcola 0:f1d3878b8dd9 136 int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid)
vpcola 0:f1d3878b8dd9 137 {
vpcola 0:f1d3878b8dd9 138 return MQTTSerialize_ack(buf, buflen, PUBACK, packetid, 0);
vpcola 0:f1d3878b8dd9 139 }
vpcola 0:f1d3878b8dd9 140
vpcola 0:f1d3878b8dd9 141
vpcola 0:f1d3878b8dd9 142 /**
vpcola 0:f1d3878b8dd9 143 * Serializes a pubrel packet into the supplied buffer.
vpcola 0:f1d3878b8dd9 144 * @param buf the buffer into which the packet will be serialized
vpcola 0:f1d3878b8dd9 145 * @param buflen the length in bytes of the supplied buffer
vpcola 0:f1d3878b8dd9 146 * @param dup integer - the MQTT dup flag
vpcola 0:f1d3878b8dd9 147 * @param packetid integer - the MQTT packet identifier
vpcola 0:f1d3878b8dd9 148 * @return serialized length, or error if 0
vpcola 0:f1d3878b8dd9 149 */
vpcola 0:f1d3878b8dd9 150 int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid)
vpcola 0:f1d3878b8dd9 151 {
vpcola 0:f1d3878b8dd9 152 return MQTTSerialize_ack(buf, buflen, PUBREL, packetid, dup);
vpcola 0:f1d3878b8dd9 153 }
vpcola 0:f1d3878b8dd9 154
vpcola 0:f1d3878b8dd9 155
vpcola 0:f1d3878b8dd9 156 /**
vpcola 0:f1d3878b8dd9 157 * Serializes a pubrel packet into the supplied buffer.
vpcola 0:f1d3878b8dd9 158 * @param buf the buffer into which the packet will be serialized
vpcola 0:f1d3878b8dd9 159 * @param buflen the length in bytes of the supplied buffer
vpcola 0:f1d3878b8dd9 160 * @param packetid integer - the MQTT packet identifier
vpcola 0:f1d3878b8dd9 161 * @return serialized length, or error if 0
vpcola 0:f1d3878b8dd9 162 */
vpcola 0:f1d3878b8dd9 163 int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid)
vpcola 0:f1d3878b8dd9 164 {
vpcola 0:f1d3878b8dd9 165 return MQTTSerialize_ack(buf, buflen, PUBCOMP, packetid, 0);
vpcola 0:f1d3878b8dd9 166 }
vpcola 0:f1d3878b8dd9 167
vpcola 0:f1d3878b8dd9 168