123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #include <MQTTPacket.h>
- #include <string.h>
- #include <StackTrace.h>
- int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[])
- {
- int i;
- int len = 2;
- for (i = 0; i < count; ++i)
- len += 2 + MQTTstrlen(topicFilters[i]) + 1;
- return len;
- }
- int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count,
- MQTTString topicFilters[], int requestedQoSs[])
- {
- unsigned char *ptr = buf;
- MQTTHeader header = {0};
- int rem_len = 0;
- int rc = 0;
- int i = 0;
- FUNC_ENTRY;
- if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen)
- {
- rc = MQTTPACKET_BUFFER_TOO_SHORT;
- goto exit;
- }
- header.byte = 0;
- header.bits.type = SUBSCRIBE;
- header.bits.dup = dup;
- header.bits.qos = 1;
- writeChar(&ptr, header.byte);
- ptr += MQTTPacket_encode(ptr, rem_len); ;
- writeInt(&ptr, packetid);
- for (i = 0; i < count; ++i)
- {
- writeMQTTString(&ptr, topicFilters[i]);
- writeChar(&ptr, requestedQoSs[i]);
- }
- rc = ptr - buf;
- exit:
- FUNC_EXIT_RC(rc);
- return rc;
- }
- int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen)
- {
- MQTTHeader header = {0};
- unsigned char* curdata = buf;
- unsigned char* enddata = NULL;
- int rc = 0;
- int mylen;
- FUNC_ENTRY;
- header.byte = readChar(&curdata);
- if (header.bits.type != SUBACK)
- goto exit;
- curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen));
- enddata = curdata + mylen;
- if (enddata - curdata < 2)
- goto exit;
- *packetid = readInt(&curdata);
- *count = 0;
- while (curdata < enddata)
- {
- if (*count > maxcount)
- {
- rc = -1;
- goto exit;
- }
- grantedQoSs[(*count)++] = readChar(&curdata);
- }
- rc = 1;
- exit:
- FUNC_EXIT_RC(rc);
- return rc;
- }
|