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