My Modify MQTTPacket Packet size 100 -> 400
Fork of MQTTPacket by
MQTTSerializePublish.c@20:18bc6c007ce2, 2015-08-12 (annotated)
- Committer:
- ericliang
- Date:
- Wed Aug 12 02:38:26 2015 +0000
- Revision:
- 20:18bc6c007ce2
- Parent:
- 17:c5bd28cc139a
Modify MQTTPacket Size to 400 Bytes
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; |
ericliang | 20:18bc6c007ce2 | 61 | printf("eric serial 1\n"); |
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 | { |
ericliang | 20:18bc6c007ce2 | 65 | printf("eric serial error payload=%d rem %d buflen=%d\n",payloadlen, rem_len, buflen); |
icraggs | 0:7734401cc1b4 | 66 | rc = MQTTPACKET_BUFFER_TOO_SHORT; |
icraggs | 0:7734401cc1b4 | 67 | goto exit; |
icraggs | 0:7734401cc1b4 | 68 | } |
ericliang | 20:18bc6c007ce2 | 69 | printf("eric serial 2\n"); |
icraggs | 0:7734401cc1b4 | 70 | header.bits.type = PUBLISH; |
icraggs | 0:7734401cc1b4 | 71 | header.bits.dup = dup; |
icraggs | 0:7734401cc1b4 | 72 | header.bits.qos = qos; |
icraggs | 0:7734401cc1b4 | 73 | header.bits.retain = retained; |
icraggs | 0:7734401cc1b4 | 74 | writeChar(&ptr, header.byte); /* write header */ |
icraggs | 0:7734401cc1b4 | 75 | |
icraggs | 0:7734401cc1b4 | 76 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; |
icraggs | 0:7734401cc1b4 | 77 | |
icraggs | 0:7734401cc1b4 | 78 | writeMQTTString(&ptr, topicName); |
icraggs | 0:7734401cc1b4 | 79 | |
icraggs | 0:7734401cc1b4 | 80 | if (qos > 0) |
icraggs | 0:7734401cc1b4 | 81 | writeInt(&ptr, packetid); |
icraggs | 0:7734401cc1b4 | 82 | |
icraggs | 0:7734401cc1b4 | 83 | memcpy(ptr, payload, payloadlen); |
icraggs | 0:7734401cc1b4 | 84 | ptr += payloadlen; |
icraggs | 0:7734401cc1b4 | 85 | |
icraggs | 0:7734401cc1b4 | 86 | rc = ptr - buf; |
icraggs | 0:7734401cc1b4 | 87 | |
icraggs | 0:7734401cc1b4 | 88 | exit: |
icraggs | 0:7734401cc1b4 | 89 | FUNC_EXIT_RC(rc); |
icraggs | 0:7734401cc1b4 | 90 | return rc; |
icraggs | 0:7734401cc1b4 | 91 | } |
icraggs | 0:7734401cc1b4 | 92 | |
icraggs | 0:7734401cc1b4 | 93 | |
icraggs | 0:7734401cc1b4 | 94 | |
icraggs | 0:7734401cc1b4 | 95 | /** |
icraggs | 0:7734401cc1b4 | 96 | * Serializes the ack packet into the supplied buffer. |
icraggs | 0:7734401cc1b4 | 97 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:7734401cc1b4 | 98 | * @param buflen the length in bytes of the supplied buffer |
Ian Craggs |
14:c2052aee81de | 99 | * @param type the MQTT packet type |
Ian Craggs |
14:c2052aee81de | 100 | * @param dup the MQTT dup flag |
Ian Craggs |
14:c2052aee81de | 101 | * @param packetid the MQTT packet identifier |
icraggs | 0:7734401cc1b4 | 102 | * @return serialized length, or error if 0 |
icraggs | 0:7734401cc1b4 | 103 | */ |
Ian Craggs |
14:c2052aee81de | 104 | int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid) |
icraggs | 0:7734401cc1b4 | 105 | { |
icraggs | 17:c5bd28cc139a | 106 | MQTTHeader header = {0}; |
icraggs | 0:7734401cc1b4 | 107 | int rc = 0; |
Ian Craggs |
14:c2052aee81de | 108 | unsigned char *ptr = buf; |
icraggs | 0:7734401cc1b4 | 109 | |
icraggs | 0:7734401cc1b4 | 110 | FUNC_ENTRY; |
icraggs | 0:7734401cc1b4 | 111 | if (buflen < 4) |
icraggs | 0:7734401cc1b4 | 112 | { |
icraggs | 0:7734401cc1b4 | 113 | rc = MQTTPACKET_BUFFER_TOO_SHORT; |
icraggs | 0:7734401cc1b4 | 114 | goto exit; |
icraggs | 0:7734401cc1b4 | 115 | } |
Ian Craggs |
14:c2052aee81de | 116 | header.bits.type = packettype; |
icraggs | 0:7734401cc1b4 | 117 | header.bits.dup = dup; |
icraggs | 0:7734401cc1b4 | 118 | header.bits.qos = 0; |
icraggs | 0:7734401cc1b4 | 119 | writeChar(&ptr, header.byte); /* write header */ |
icraggs | 0:7734401cc1b4 | 120 | |
icraggs | 0:7734401cc1b4 | 121 | ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */ |
icraggs | 0:7734401cc1b4 | 122 | writeInt(&ptr, packetid); |
icraggs | 0:7734401cc1b4 | 123 | rc = ptr - buf; |
icraggs | 0:7734401cc1b4 | 124 | exit: |
icraggs | 0:7734401cc1b4 | 125 | FUNC_EXIT_RC(rc); |
icraggs | 0:7734401cc1b4 | 126 | return rc; |
icraggs | 0:7734401cc1b4 | 127 | } |
icraggs | 0:7734401cc1b4 | 128 | |
icraggs | 0:7734401cc1b4 | 129 | |
icraggs | 0:7734401cc1b4 | 130 | /** |
icraggs | 0:7734401cc1b4 | 131 | * Serializes a puback packet into the supplied buffer. |
icraggs | 0:7734401cc1b4 | 132 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:7734401cc1b4 | 133 | * @param buflen the length in bytes of the supplied buffer |
icraggs | 0:7734401cc1b4 | 134 | * @param packetid integer - the MQTT packet identifier |
icraggs | 0:7734401cc1b4 | 135 | * @return serialized length, or error if 0 |
icraggs | 0:7734401cc1b4 | 136 | */ |
Ian Craggs |
14:c2052aee81de | 137 | int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid) |
icraggs | 0:7734401cc1b4 | 138 | { |
icraggs | 0:7734401cc1b4 | 139 | return MQTTSerialize_ack(buf, buflen, PUBACK, packetid, 0); |
icraggs | 0:7734401cc1b4 | 140 | } |
icraggs | 0:7734401cc1b4 | 141 | |
icraggs | 0:7734401cc1b4 | 142 | |
icraggs | 0:7734401cc1b4 | 143 | /** |
icraggs | 0:7734401cc1b4 | 144 | * Serializes a pubrel packet into the supplied buffer. |
icraggs | 0:7734401cc1b4 | 145 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:7734401cc1b4 | 146 | * @param buflen the length in bytes of the supplied buffer |
icraggs | 0:7734401cc1b4 | 147 | * @param dup integer - the MQTT dup flag |
icraggs | 0:7734401cc1b4 | 148 | * @param packetid integer - the MQTT packet identifier |
icraggs | 0:7734401cc1b4 | 149 | * @return serialized length, or error if 0 |
icraggs | 0:7734401cc1b4 | 150 | */ |
Ian Craggs |
14:c2052aee81de | 151 | int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid) |
icraggs | 0:7734401cc1b4 | 152 | { |
icraggs | 0:7734401cc1b4 | 153 | return MQTTSerialize_ack(buf, buflen, PUBREL, packetid, dup); |
icraggs | 0:7734401cc1b4 | 154 | } |
icraggs | 0:7734401cc1b4 | 155 | |
icraggs | 0:7734401cc1b4 | 156 | |
icraggs | 0:7734401cc1b4 | 157 | /** |
icraggs | 0:7734401cc1b4 | 158 | * Serializes a pubrel packet into the supplied buffer. |
icraggs | 0:7734401cc1b4 | 159 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:7734401cc1b4 | 160 | * @param buflen the length in bytes of the supplied buffer |
icraggs | 0:7734401cc1b4 | 161 | * @param packetid integer - the MQTT packet identifier |
icraggs | 0:7734401cc1b4 | 162 | * @return serialized length, or error if 0 |
icraggs | 0:7734401cc1b4 | 163 | */ |
Ian Craggs |
14:c2052aee81de | 164 | int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid) |
icraggs | 0:7734401cc1b4 | 165 | { |
icraggs | 0:7734401cc1b4 | 166 | return MQTTSerialize_ack(buf, buflen, PUBCOMP, packetid, 0); |
icraggs | 0:7734401cc1b4 | 167 | } |
icraggs | 0:7734401cc1b4 | 168 | |
icraggs | 0:7734401cc1b4 | 169 |