ME910 support
Dependents: 5_Dragonfly_Cayenne_Sprint_IKS01A1
Fork of Cayenne-MQTT-mbed-MTSAS by
src/MQTTCommon/MQTTSubscribeClient.c@22:0dbabcc6e7b2, 2017-01-25 (annotated)
- Committer:
- jburhenn
- Date:
- Wed Jan 25 10:34:16 2017 -0700
- Branch:
- feature/multivalue
- Revision:
- 22:0dbabcc6e7b2
- Parent:
- 0:09ef59d2d0f7
Added support for multi-value arrays and size_t data type modifications from the Cayenne-MQTT-C library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jburhenn | 0:09ef59d2d0f7 | 1 | /******************************************************************************* |
jburhenn | 0:09ef59d2d0f7 | 2 | * Copyright (c) 2014 IBM Corp. |
jburhenn | 0:09ef59d2d0f7 | 3 | * |
jburhenn | 0:09ef59d2d0f7 | 4 | * All rights reserved. This program and the accompanying materials |
jburhenn | 0:09ef59d2d0f7 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
jburhenn | 0:09ef59d2d0f7 | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
jburhenn | 0:09ef59d2d0f7 | 7 | * |
jburhenn | 0:09ef59d2d0f7 | 8 | * The Eclipse Public License is available at |
jburhenn | 0:09ef59d2d0f7 | 9 | * http://www.eclipse.org/legal/epl-v10.html |
jburhenn | 0:09ef59d2d0f7 | 10 | * and the Eclipse Distribution License is available at |
jburhenn | 0:09ef59d2d0f7 | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
jburhenn | 0:09ef59d2d0f7 | 12 | * |
jburhenn | 0:09ef59d2d0f7 | 13 | * Contributors: |
jburhenn | 0:09ef59d2d0f7 | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
jburhenn | 0:09ef59d2d0f7 | 15 | *******************************************************************************/ |
jburhenn | 0:09ef59d2d0f7 | 16 | |
jburhenn | 0:09ef59d2d0f7 | 17 | #include "MQTTPacket.h" |
jburhenn | 0:09ef59d2d0f7 | 18 | |
jburhenn | 0:09ef59d2d0f7 | 19 | #include <string.h> |
jburhenn | 0:09ef59d2d0f7 | 20 | |
jburhenn | 0:09ef59d2d0f7 | 21 | /** |
jburhenn | 0:09ef59d2d0f7 | 22 | * Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters |
jburhenn | 0:09ef59d2d0f7 | 23 | * @param count the number of topic filter strings in topicFilters |
jburhenn | 0:09ef59d2d0f7 | 24 | * @param topicFilters the array of topic filter strings to be used in the publish |
jburhenn | 0:09ef59d2d0f7 | 25 | * @return the length of buffer needed to contain the serialized version of the packet |
jburhenn | 0:09ef59d2d0f7 | 26 | */ |
jburhenn | 22:0dbabcc6e7b2 | 27 | size_t MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[]) |
jburhenn | 0:09ef59d2d0f7 | 28 | { |
jburhenn | 0:09ef59d2d0f7 | 29 | int i; |
jburhenn | 22:0dbabcc6e7b2 | 30 | size_t len = 2; /* packetid */ |
jburhenn | 0:09ef59d2d0f7 | 31 | |
jburhenn | 0:09ef59d2d0f7 | 32 | for (i = 0; i < count; ++i) |
jburhenn | 0:09ef59d2d0f7 | 33 | len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */ |
jburhenn | 0:09ef59d2d0f7 | 34 | return len; |
jburhenn | 0:09ef59d2d0f7 | 35 | } |
jburhenn | 0:09ef59d2d0f7 | 36 | |
jburhenn | 0:09ef59d2d0f7 | 37 | |
jburhenn | 0:09ef59d2d0f7 | 38 | /** |
jburhenn | 0:09ef59d2d0f7 | 39 | * Serializes the supplied subscribe data into the supplied buffer, ready for sending |
jburhenn | 0:09ef59d2d0f7 | 40 | * @param buf the buffer into which the packet will be serialized |
jburhenn | 0:09ef59d2d0f7 | 41 | * @param buflen the length in bytes of the supplied bufferr |
jburhenn | 0:09ef59d2d0f7 | 42 | * @param dup integer - the MQTT dup flag |
jburhenn | 0:09ef59d2d0f7 | 43 | * @param packetid integer - the MQTT packet identifier |
jburhenn | 0:09ef59d2d0f7 | 44 | * @param count - number of members in the topicFilters and reqQos arrays |
jburhenn | 0:09ef59d2d0f7 | 45 | * @param topicFilters - array of topic filter names |
jburhenn | 0:09ef59d2d0f7 | 46 | * @param requestedQoSs - array of requested QoS |
jburhenn | 0:09ef59d2d0f7 | 47 | * @return the length of the serialized data. <= 0 indicates error |
jburhenn | 0:09ef59d2d0f7 | 48 | */ |
jburhenn | 22:0dbabcc6e7b2 | 49 | int MQTTSerialize_subscribe(unsigned char* buf, size_t buflen, unsigned char dup, unsigned short packetid, int count, |
jburhenn | 0:09ef59d2d0f7 | 50 | MQTTString topicFilters[], int requestedQoSs[]) |
jburhenn | 0:09ef59d2d0f7 | 51 | { |
jburhenn | 0:09ef59d2d0f7 | 52 | unsigned char *ptr = buf; |
jburhenn | 0:09ef59d2d0f7 | 53 | MQTTHeader header = {0}; |
jburhenn | 22:0dbabcc6e7b2 | 54 | size_t rem_len = 0; |
jburhenn | 0:09ef59d2d0f7 | 55 | int rc = 0; |
jburhenn | 0:09ef59d2d0f7 | 56 | int i = 0; |
jburhenn | 0:09ef59d2d0f7 | 57 | |
jburhenn | 0:09ef59d2d0f7 | 58 | if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen) |
jburhenn | 0:09ef59d2d0f7 | 59 | { |
jburhenn | 0:09ef59d2d0f7 | 60 | rc = MQTTPACKET_BUFFER_TOO_SHORT; |
jburhenn | 0:09ef59d2d0f7 | 61 | goto exit; |
jburhenn | 0:09ef59d2d0f7 | 62 | } |
jburhenn | 0:09ef59d2d0f7 | 63 | |
jburhenn | 0:09ef59d2d0f7 | 64 | header.byte = 0; |
jburhenn | 0:09ef59d2d0f7 | 65 | header.bits.type = SUBSCRIBE_MSG; |
jburhenn | 0:09ef59d2d0f7 | 66 | header.bits.dup = dup; |
jburhenn | 0:09ef59d2d0f7 | 67 | header.bits.qos = 1; |
jburhenn | 0:09ef59d2d0f7 | 68 | writeChar(&ptr, header.byte); /* write header */ |
jburhenn | 0:09ef59d2d0f7 | 69 | |
jburhenn | 0:09ef59d2d0f7 | 70 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; |
jburhenn | 0:09ef59d2d0f7 | 71 | |
jburhenn | 0:09ef59d2d0f7 | 72 | writeInt(&ptr, packetid); |
jburhenn | 0:09ef59d2d0f7 | 73 | |
jburhenn | 0:09ef59d2d0f7 | 74 | for (i = 0; i < count; ++i) |
jburhenn | 0:09ef59d2d0f7 | 75 | { |
jburhenn | 0:09ef59d2d0f7 | 76 | writeMQTTString(&ptr, topicFilters[i]); |
jburhenn | 0:09ef59d2d0f7 | 77 | writeChar(&ptr, requestedQoSs[i]); |
jburhenn | 0:09ef59d2d0f7 | 78 | } |
jburhenn | 0:09ef59d2d0f7 | 79 | |
jburhenn | 22:0dbabcc6e7b2 | 80 | rc = (int)(ptr - buf); |
jburhenn | 0:09ef59d2d0f7 | 81 | exit: |
jburhenn | 0:09ef59d2d0f7 | 82 | return rc; |
jburhenn | 0:09ef59d2d0f7 | 83 | } |
jburhenn | 0:09ef59d2d0f7 | 84 | |
jburhenn | 0:09ef59d2d0f7 | 85 | |
jburhenn | 0:09ef59d2d0f7 | 86 | |
jburhenn | 0:09ef59d2d0f7 | 87 | /** |
jburhenn | 0:09ef59d2d0f7 | 88 | * Deserializes the supplied (wire) buffer into suback data |
jburhenn | 0:09ef59d2d0f7 | 89 | * @param packetid returned integer - the MQTT packet identifier |
jburhenn | 0:09ef59d2d0f7 | 90 | * @param maxcount - the maximum number of members allowed in the grantedQoSs array |
jburhenn | 0:09ef59d2d0f7 | 91 | * @param count returned integer - number of members in the grantedQoSs array |
jburhenn | 0:09ef59d2d0f7 | 92 | * @param grantedQoSs returned array of integers - the granted qualities of service |
jburhenn | 0:09ef59d2d0f7 | 93 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
jburhenn | 0:09ef59d2d0f7 | 94 | * @param buflen the length in bytes of the data in the supplied buffer |
jburhenn | 0:09ef59d2d0f7 | 95 | * @return error code. 1 is success, 0 is failure |
jburhenn | 0:09ef59d2d0f7 | 96 | */ |
jburhenn | 22:0dbabcc6e7b2 | 97 | int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, size_t buflen) |
jburhenn | 0:09ef59d2d0f7 | 98 | { |
jburhenn | 0:09ef59d2d0f7 | 99 | MQTTHeader header = {0}; |
jburhenn | 0:09ef59d2d0f7 | 100 | unsigned char* curdata = buf; |
jburhenn | 0:09ef59d2d0f7 | 101 | unsigned char* enddata = NULL; |
jburhenn | 0:09ef59d2d0f7 | 102 | int rc = 0; |
jburhenn | 0:09ef59d2d0f7 | 103 | int mylen; |
jburhenn | 0:09ef59d2d0f7 | 104 | |
jburhenn | 0:09ef59d2d0f7 | 105 | header.byte = readChar(&curdata); |
jburhenn | 0:09ef59d2d0f7 | 106 | if (header.bits.type != SUBACK_MSG) |
jburhenn | 0:09ef59d2d0f7 | 107 | goto exit; |
jburhenn | 0:09ef59d2d0f7 | 108 | |
jburhenn | 0:09ef59d2d0f7 | 109 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ |
jburhenn | 0:09ef59d2d0f7 | 110 | enddata = curdata + mylen; |
jburhenn | 0:09ef59d2d0f7 | 111 | if (enddata - curdata < 2) |
jburhenn | 0:09ef59d2d0f7 | 112 | goto exit; |
jburhenn | 0:09ef59d2d0f7 | 113 | |
jburhenn | 0:09ef59d2d0f7 | 114 | *packetid = readInt(&curdata); |
jburhenn | 0:09ef59d2d0f7 | 115 | |
jburhenn | 0:09ef59d2d0f7 | 116 | *count = 0; |
jburhenn | 0:09ef59d2d0f7 | 117 | while (curdata < enddata) |
jburhenn | 0:09ef59d2d0f7 | 118 | { |
jburhenn | 0:09ef59d2d0f7 | 119 | if (*count > maxcount) |
jburhenn | 0:09ef59d2d0f7 | 120 | { |
jburhenn | 0:09ef59d2d0f7 | 121 | rc = -1; |
jburhenn | 0:09ef59d2d0f7 | 122 | goto exit; |
jburhenn | 0:09ef59d2d0f7 | 123 | } |
jburhenn | 0:09ef59d2d0f7 | 124 | grantedQoSs[(*count)++] = readChar(&curdata); |
jburhenn | 0:09ef59d2d0f7 | 125 | } |
jburhenn | 0:09ef59d2d0f7 | 126 | |
jburhenn | 0:09ef59d2d0f7 | 127 | rc = 1; |
jburhenn | 0:09ef59d2d0f7 | 128 | exit: |
jburhenn | 0:09ef59d2d0f7 | 129 | return rc; |
jburhenn | 0:09ef59d2d0f7 | 130 | } |
jburhenn | 0:09ef59d2d0f7 | 131 | |
jburhenn | 0:09ef59d2d0f7 | 132 |