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