MQTTSubscribeClient.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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
  15. *documentation
  16. *******************************************************************************/
  17. #include "MQTTPacket.h"
  18. #include "StackTrace.h"
  19. #include <string.h>
  20. /**
  21. * Determines the length of the MQTT subscribe packet that would be produced
  22. * using the supplied parameters
  23. * @param count the number of topic filter strings in topicFilters
  24. * @param topicFilters the array of topic filter strings to be used in the
  25. * publish
  26. * @return the length of buffer needed to contain the serialized version of
  27. * the packet
  28. */
  29. int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[]) {
  30. int i;
  31. int len = 2; /* packetid */
  32. for (i = 0; i < count; ++i)
  33. len +=
  34. 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */
  35. return len;
  36. }
  37. /**
  38. * Serializes the supplied subscribe data into the supplied buffer, ready for
  39. * sending
  40. * @param buf the buffer into which the packet will be serialized
  41. * @param buflen the length in bytes of the supplied bufferr
  42. * @param dup integer - the MQTT dup flag
  43. * @param packetid integer - the MQTT packet identifier
  44. * @param count - number of members in the topicFilters and reqQos arrays
  45. * @param topicFilters - array of topic filter names
  46. * @param requestedQoSs - array of requested QoS
  47. * @return the length of the serialized data. <= 0 indicates error
  48. */
  49. int MQTTSerialize_subscribe(unsigned char* buf,
  50. int buflen,
  51. unsigned char dup,
  52. unsigned short packetid,
  53. int count,
  54. MQTTString topicFilters[],
  55. int requestedQoSs[]) {
  56. unsigned char* ptr = buf;
  57. MQTTHeader header = {0};
  58. int rem_len = 0;
  59. int rc = 0;
  60. int i = 0;
  61. FUNC_ENTRY;
  62. if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(
  63. count, topicFilters)) > buflen) {
  64. rc = MQTTPACKET_BUFFER_TOO_SHORT;
  65. goto exit;
  66. }
  67. header.byte = 0;
  68. header.bits.type = SUBSCRIBE;
  69. header.bits.dup = dup;
  70. header.bits.qos = 1;
  71. writeChar(&ptr, header.byte); /* write header */
  72. ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */
  73. ;
  74. writeInt(&ptr, packetid);
  75. for (i = 0; i < count; ++i) {
  76. writeMQTTString(&ptr, topicFilters[i]);
  77. writeChar(&ptr, requestedQoSs[i]);
  78. }
  79. rc = ptr - buf;
  80. exit:
  81. FUNC_EXIT_RC(rc);
  82. return rc;
  83. }
  84. /**
  85. * Deserializes the supplied (wire) buffer into suback data
  86. * @param packetid returned integer - the MQTT packet identifier
  87. * @param maxcount - the maximum number of members allowed in the grantedQoSs
  88. * array
  89. * @param count returned integer - number of members in the grantedQoSs array
  90. * @param grantedQoSs returned array of integers - the granted qualities of
  91. * service
  92. * @param buf the raw buffer data, of the correct length determined by the
  93. * remaining length field
  94. * @param buflen the length in bytes of the data in the supplied buffer
  95. * @return error code. 1 is success, 0 is failure
  96. */
  97. int MQTTDeserialize_suback(unsigned short* packetid,
  98. int maxcount,
  99. int* count,
  100. int grantedQoSs[],
  101. unsigned char* buf,
  102. int buflen) {
  103. MQTTHeader header = {0};
  104. unsigned char* curdata = buf;
  105. unsigned char* enddata = NULL;
  106. int rc = 0;
  107. int mylen;
  108. FUNC_ENTRY;
  109. header.byte = readChar(&curdata);
  110. if (header.bits.type != SUBACK)
  111. goto exit;
  112. curdata += (rc = MQTTPacket_decodeBuf(curdata,
  113. &mylen)); /* read remaining length */
  114. enddata = curdata + mylen;
  115. if (enddata - curdata < 2)
  116. goto exit;
  117. *packetid = readInt(&curdata);
  118. *count = 0;
  119. while (curdata < enddata) {
  120. if (*count > maxcount) {
  121. rc = -1;
  122. goto exit;
  123. }
  124. grantedQoSs[(*count)++] = readChar(&curdata);
  125. }
  126. rc = 1;
  127. exit:
  128. FUNC_EXIT_RC(rc);
  129. return rc;
  130. }