Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MQTTPacket by
MQTTSubscribeClient.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 | * Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters |
icraggs | 0:7734401cc1b4 | 24 | * @param count the number of topic filter strings in topicFilters |
icraggs | 0:7734401cc1b4 | 25 | * @param topicFilters the array of topic filter strings to be used in the publish |
icraggs | 0:7734401cc1b4 | 26 | * @return the length of buffer needed to contain the serialized version of the packet |
icraggs | 0:7734401cc1b4 | 27 | */ |
icraggs | 0:7734401cc1b4 | 28 | int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[]) |
icraggs | 0:7734401cc1b4 | 29 | { |
icraggs | 0:7734401cc1b4 | 30 | int i; |
icraggs | 0:7734401cc1b4 | 31 | int len = 2; /* packetid */ |
icraggs | 0:7734401cc1b4 | 32 | |
icraggs | 0:7734401cc1b4 | 33 | for (i = 0; i < count; ++i) |
icraggs | 0:7734401cc1b4 | 34 | len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */ |
icraggs | 0:7734401cc1b4 | 35 | return len; |
icraggs | 0:7734401cc1b4 | 36 | } |
icraggs | 0:7734401cc1b4 | 37 | |
icraggs | 0:7734401cc1b4 | 38 | |
icraggs | 0:7734401cc1b4 | 39 | /** |
icraggs | 0:7734401cc1b4 | 40 | * Serializes the supplied subscribe data into the supplied buffer, ready for sending |
icraggs | 0:7734401cc1b4 | 41 | * @param buf the buffer into which the packet will be serialized |
icraggs | 0:7734401cc1b4 | 42 | * @param buflen the length in bytes of the supplied bufferr |
icraggs | 0:7734401cc1b4 | 43 | * @param dup integer - the MQTT dup flag |
icraggs | 0:7734401cc1b4 | 44 | * @param packetid integer - the MQTT packet identifier |
icraggs | 0:7734401cc1b4 | 45 | * @param count - number of members in the topicFilters and reqQos arrays |
icraggs | 0:7734401cc1b4 | 46 | * @param topicFilters - array of topic filter names |
icraggs | 0:7734401cc1b4 | 47 | * @param requestedQoSs - array of requested QoS |
icraggs | 0:7734401cc1b4 | 48 | * @return the length of the serialized data. <= 0 indicates error |
icraggs | 0:7734401cc1b4 | 49 | */ |
Ian Craggs |
14:c2052aee81de | 50 | int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count, |
Ian Craggs |
14:c2052aee81de | 51 | MQTTString topicFilters[], int requestedQoSs[]) |
icraggs | 0:7734401cc1b4 | 52 | { |
Ian Craggs |
14:c2052aee81de | 53 | unsigned char *ptr = buf; |
icraggs | 17:c5bd28cc139a | 54 | MQTTHeader header = {0}; |
icraggs | 0:7734401cc1b4 | 55 | int rem_len = 0; |
icraggs | 0:7734401cc1b4 | 56 | int rc = 0; |
icraggs | 0:7734401cc1b4 | 57 | int i = 0; |
icraggs | 0:7734401cc1b4 | 58 | |
icraggs | 0:7734401cc1b4 | 59 | FUNC_ENTRY; |
icraggs | 0:7734401cc1b4 | 60 | if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen) |
icraggs | 0:7734401cc1b4 | 61 | { |
icraggs | 0:7734401cc1b4 | 62 | rc = MQTTPACKET_BUFFER_TOO_SHORT; |
icraggs | 0:7734401cc1b4 | 63 | goto exit; |
icraggs | 0:7734401cc1b4 | 64 | } |
icraggs | 0:7734401cc1b4 | 65 | |
icraggs | 0:7734401cc1b4 | 66 | header.byte = 0; |
icraggs | 0:7734401cc1b4 | 67 | header.bits.type = SUBSCRIBE; |
icraggs | 0:7734401cc1b4 | 68 | header.bits.dup = dup; |
icraggs | 0:7734401cc1b4 | 69 | header.bits.qos = 1; |
icraggs | 0:7734401cc1b4 | 70 | writeChar(&ptr, header.byte); /* write header */ |
icraggs | 0:7734401cc1b4 | 71 | |
icraggs | 0:7734401cc1b4 | 72 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; |
icraggs | 0:7734401cc1b4 | 73 | |
icraggs | 0:7734401cc1b4 | 74 | writeInt(&ptr, packetid); |
icraggs | 0:7734401cc1b4 | 75 | |
icraggs | 0:7734401cc1b4 | 76 | for (i = 0; i < count; ++i) |
icraggs | 0:7734401cc1b4 | 77 | { |
icraggs | 0:7734401cc1b4 | 78 | writeMQTTString(&ptr, topicFilters[i]); |
icraggs | 0:7734401cc1b4 | 79 | writeChar(&ptr, requestedQoSs[i]); |
icraggs | 0:7734401cc1b4 | 80 | } |
icraggs | 0:7734401cc1b4 | 81 | |
icraggs | 0:7734401cc1b4 | 82 | rc = ptr - buf; |
icraggs | 0:7734401cc1b4 | 83 | exit: |
icraggs | 0:7734401cc1b4 | 84 | FUNC_EXIT_RC(rc); |
icraggs | 0:7734401cc1b4 | 85 | return rc; |
icraggs | 0:7734401cc1b4 | 86 | } |
icraggs | 0:7734401cc1b4 | 87 | |
icraggs | 0:7734401cc1b4 | 88 | |
icraggs | 0:7734401cc1b4 | 89 | |
icraggs | 0:7734401cc1b4 | 90 | /** |
icraggs | 0:7734401cc1b4 | 91 | * Deserializes the supplied (wire) buffer into suback data |
icraggs | 0:7734401cc1b4 | 92 | * @param packetid returned integer - the MQTT packet identifier |
icraggs | 0:7734401cc1b4 | 93 | * @param maxcount - the maximum number of members allowed in the grantedQoSs array |
icraggs | 0:7734401cc1b4 | 94 | * @param count returned integer - number of members in the grantedQoSs array |
icraggs | 0:7734401cc1b4 | 95 | * @param grantedQoSs returned array of integers - the granted qualities of service |
icraggs | 0:7734401cc1b4 | 96 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
icraggs | 0:7734401cc1b4 | 97 | * @param buflen the length in bytes of the data in the supplied buffer |
icraggs | 0:7734401cc1b4 | 98 | * @return error code. 1 is success, 0 is failure |
icraggs | 0:7734401cc1b4 | 99 | */ |
Ian Craggs |
14:c2052aee81de | 100 | int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen) |
icraggs | 0:7734401cc1b4 | 101 | { |
icraggs | 17:c5bd28cc139a | 102 | MQTTHeader header = {0}; |
Ian Craggs |
14:c2052aee81de | 103 | unsigned char* curdata = buf; |
Ian Craggs |
14:c2052aee81de | 104 | unsigned char* enddata = NULL; |
icraggs | 0:7734401cc1b4 | 105 | int rc = 0; |
icraggs | 0:7734401cc1b4 | 106 | int mylen; |
icraggs | 0:7734401cc1b4 | 107 | |
icraggs | 0:7734401cc1b4 | 108 | FUNC_ENTRY; |
icraggs | 0:7734401cc1b4 | 109 | header.byte = readChar(&curdata); |
icraggs | 16:d0b3886ada32 | 110 | if (header.bits.type != SUBACK) |
icraggs | 16:d0b3886ada32 | 111 | goto exit; |
icraggs | 0:7734401cc1b4 | 112 | |
icraggs | 0:7734401cc1b4 | 113 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ |
icraggs | 0:7734401cc1b4 | 114 | enddata = curdata + mylen; |
icraggs | 0:7734401cc1b4 | 115 | if (enddata - curdata < 2) |
icraggs | 0:7734401cc1b4 | 116 | goto exit; |
icraggs | 0:7734401cc1b4 | 117 | |
icraggs | 0:7734401cc1b4 | 118 | *packetid = readInt(&curdata); |
icraggs | 0:7734401cc1b4 | 119 | |
icraggs | 0:7734401cc1b4 | 120 | *count = 0; |
icraggs | 0:7734401cc1b4 | 121 | while (curdata < enddata) |
icraggs | 0:7734401cc1b4 | 122 | { |
icraggs | 0:7734401cc1b4 | 123 | if (*count > maxcount) |
icraggs | 0:7734401cc1b4 | 124 | { |
icraggs | 0:7734401cc1b4 | 125 | rc = -1; |
icraggs | 0:7734401cc1b4 | 126 | goto exit; |
icraggs | 0:7734401cc1b4 | 127 | } |
icraggs | 0:7734401cc1b4 | 128 | grantedQoSs[(*count)++] = readChar(&curdata); |
icraggs | 0:7734401cc1b4 | 129 | } |
icraggs | 0:7734401cc1b4 | 130 | |
icraggs | 0:7734401cc1b4 | 131 | rc = 1; |
icraggs | 0:7734401cc1b4 | 132 | exit: |
icraggs | 0:7734401cc1b4 | 133 | FUNC_EXIT_RC(rc); |
icraggs | 0:7734401cc1b4 | 134 | return rc; |
icraggs | 0:7734401cc1b4 | 135 | } |
icraggs | 0:7734401cc1b4 | 136 | |
icraggs | 0:7734401cc1b4 | 137 |