Version to make it easier to reuse without source modifications
MQTTPacket/MQTTSubscribeServer.c@0:5cd4781e0c88, 2018-03-27 (annotated)
- Committer:
- JMF
- Date:
- Tue Mar 27 17:26:35 2018 +0000
- Revision:
- 0:5cd4781e0c88
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:5cd4781e0c88 | 1 | /******************************************************************************* |
JMF | 0:5cd4781e0c88 | 2 | * Copyright (c) 2014 IBM Corp. |
JMF | 0:5cd4781e0c88 | 3 | * |
JMF | 0:5cd4781e0c88 | 4 | * All rights reserved. This program and the accompanying materials |
JMF | 0:5cd4781e0c88 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
JMF | 0:5cd4781e0c88 | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
JMF | 0:5cd4781e0c88 | 7 | * |
JMF | 0:5cd4781e0c88 | 8 | * The Eclipse Public License is available at |
JMF | 0:5cd4781e0c88 | 9 | * http://www.eclipse.org/legal/epl-v10.html |
JMF | 0:5cd4781e0c88 | 10 | * and the Eclipse Distribution License is available at |
JMF | 0:5cd4781e0c88 | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
JMF | 0:5cd4781e0c88 | 12 | * |
JMF | 0:5cd4781e0c88 | 13 | * Contributors: |
JMF | 0:5cd4781e0c88 | 14 | * Ian Craggs - initial API and implementation and/or initial documentation |
JMF | 0:5cd4781e0c88 | 15 | *******************************************************************************/ |
JMF | 0:5cd4781e0c88 | 16 | |
JMF | 0:5cd4781e0c88 | 17 | #include "MQTTPacket.h" |
JMF | 0:5cd4781e0c88 | 18 | #include "StackTrace.h" |
JMF | 0:5cd4781e0c88 | 19 | |
JMF | 0:5cd4781e0c88 | 20 | #include <string.h> |
JMF | 0:5cd4781e0c88 | 21 | |
JMF | 0:5cd4781e0c88 | 22 | |
JMF | 0:5cd4781e0c88 | 23 | /** |
JMF | 0:5cd4781e0c88 | 24 | * Deserializes the supplied (wire) buffer into subscribe data |
JMF | 0:5cd4781e0c88 | 25 | * @param dup integer returned - the MQTT dup flag |
JMF | 0:5cd4781e0c88 | 26 | * @param packetid integer returned - the MQTT packet identifier |
JMF | 0:5cd4781e0c88 | 27 | * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays |
JMF | 0:5cd4781e0c88 | 28 | * @param count - number of members in the topicFilters and requestedQoSs arrays |
JMF | 0:5cd4781e0c88 | 29 | * @param topicFilters - array of topic filter names |
JMF | 0:5cd4781e0c88 | 30 | * @param requestedQoSs - array of requested QoS |
JMF | 0:5cd4781e0c88 | 31 | * @param buf the raw buffer data, of the correct length determined by the remaining length field |
JMF | 0:5cd4781e0c88 | 32 | * @param buflen the length in bytes of the data in the supplied buffer |
JMF | 0:5cd4781e0c88 | 33 | * @return the length of the serialized data. <= 0 indicates error |
JMF | 0:5cd4781e0c88 | 34 | */ |
JMF | 0:5cd4781e0c88 | 35 | int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[], |
JMF | 0:5cd4781e0c88 | 36 | int requestedQoSs[], unsigned char* buf, int buflen) |
JMF | 0:5cd4781e0c88 | 37 | { |
JMF | 0:5cd4781e0c88 | 38 | MQTTHeader header = {0}; |
JMF | 0:5cd4781e0c88 | 39 | unsigned char* curdata = buf; |
JMF | 0:5cd4781e0c88 | 40 | unsigned char* enddata = NULL; |
JMF | 0:5cd4781e0c88 | 41 | int rc = -1; |
JMF | 0:5cd4781e0c88 | 42 | int mylen = 0; |
JMF | 0:5cd4781e0c88 | 43 | |
JMF | 0:5cd4781e0c88 | 44 | FUNC_ENTRY; |
JMF | 0:5cd4781e0c88 | 45 | header.byte = readChar(&curdata); |
JMF | 0:5cd4781e0c88 | 46 | if (header.bits.type != SUBSCRIBE) |
JMF | 0:5cd4781e0c88 | 47 | goto exit; |
JMF | 0:5cd4781e0c88 | 48 | *dup = header.bits.dup; |
JMF | 0:5cd4781e0c88 | 49 | |
JMF | 0:5cd4781e0c88 | 50 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ |
JMF | 0:5cd4781e0c88 | 51 | enddata = curdata + mylen; |
JMF | 0:5cd4781e0c88 | 52 | |
JMF | 0:5cd4781e0c88 | 53 | *packetid = readInt(&curdata); |
JMF | 0:5cd4781e0c88 | 54 | |
JMF | 0:5cd4781e0c88 | 55 | *count = 0; |
JMF | 0:5cd4781e0c88 | 56 | while (curdata < enddata) |
JMF | 0:5cd4781e0c88 | 57 | { |
JMF | 0:5cd4781e0c88 | 58 | if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata)) |
JMF | 0:5cd4781e0c88 | 59 | goto exit; |
JMF | 0:5cd4781e0c88 | 60 | if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */ |
JMF | 0:5cd4781e0c88 | 61 | goto exit; |
JMF | 0:5cd4781e0c88 | 62 | requestedQoSs[*count] = readChar(&curdata); |
JMF | 0:5cd4781e0c88 | 63 | (*count)++; |
JMF | 0:5cd4781e0c88 | 64 | } |
JMF | 0:5cd4781e0c88 | 65 | |
JMF | 0:5cd4781e0c88 | 66 | rc = 1; |
JMF | 0:5cd4781e0c88 | 67 | exit: |
JMF | 0:5cd4781e0c88 | 68 | FUNC_EXIT_RC(rc); |
JMF | 0:5cd4781e0c88 | 69 | return rc; |
JMF | 0:5cd4781e0c88 | 70 | } |
JMF | 0:5cd4781e0c88 | 71 | |
JMF | 0:5cd4781e0c88 | 72 | |
JMF | 0:5cd4781e0c88 | 73 | /** |
JMF | 0:5cd4781e0c88 | 74 | * Serializes the supplied suback data into the supplied buffer, ready for sending |
JMF | 0:5cd4781e0c88 | 75 | * @param buf the buffer into which the packet will be serialized |
JMF | 0:5cd4781e0c88 | 76 | * @param buflen the length in bytes of the supplied buffer |
JMF | 0:5cd4781e0c88 | 77 | * @param packetid integer - the MQTT packet identifier |
JMF | 0:5cd4781e0c88 | 78 | * @param count - number of members in the grantedQoSs array |
JMF | 0:5cd4781e0c88 | 79 | * @param grantedQoSs - array of granted QoS |
JMF | 0:5cd4781e0c88 | 80 | * @return the length of the serialized data. <= 0 indicates error |
JMF | 0:5cd4781e0c88 | 81 | */ |
JMF | 0:5cd4781e0c88 | 82 | int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs) |
JMF | 0:5cd4781e0c88 | 83 | { |
JMF | 0:5cd4781e0c88 | 84 | MQTTHeader header = {0}; |
JMF | 0:5cd4781e0c88 | 85 | int rc = -1; |
JMF | 0:5cd4781e0c88 | 86 | unsigned char *ptr = buf; |
JMF | 0:5cd4781e0c88 | 87 | int i; |
JMF | 0:5cd4781e0c88 | 88 | |
JMF | 0:5cd4781e0c88 | 89 | FUNC_ENTRY; |
JMF | 0:5cd4781e0c88 | 90 | if (buflen < 2 + count) |
JMF | 0:5cd4781e0c88 | 91 | { |
JMF | 0:5cd4781e0c88 | 92 | rc = MQTTPACKET_BUFFER_TOO_SHORT; |
JMF | 0:5cd4781e0c88 | 93 | goto exit; |
JMF | 0:5cd4781e0c88 | 94 | } |
JMF | 0:5cd4781e0c88 | 95 | header.byte = 0; |
JMF | 0:5cd4781e0c88 | 96 | header.bits.type = SUBACK; |
JMF | 0:5cd4781e0c88 | 97 | writeChar(&ptr, header.byte); /* write header */ |
JMF | 0:5cd4781e0c88 | 98 | |
JMF | 0:5cd4781e0c88 | 99 | ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */ |
JMF | 0:5cd4781e0c88 | 100 | |
JMF | 0:5cd4781e0c88 | 101 | writeInt(&ptr, packetid); |
JMF | 0:5cd4781e0c88 | 102 | |
JMF | 0:5cd4781e0c88 | 103 | for (i = 0; i < count; ++i) |
JMF | 0:5cd4781e0c88 | 104 | writeChar(&ptr, grantedQoSs[i]); |
JMF | 0:5cd4781e0c88 | 105 | |
JMF | 0:5cd4781e0c88 | 106 | rc = ptr - buf; |
JMF | 0:5cd4781e0c88 | 107 | exit: |
JMF | 0:5cd4781e0c88 | 108 | FUNC_EXIT_RC(rc); |
JMF | 0:5cd4781e0c88 | 109 | return rc; |
JMF | 0:5cd4781e0c88 | 110 | } |
JMF | 0:5cd4781e0c88 | 111 | |
JMF | 0:5cd4781e0c88 | 112 |