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