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