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