MQTTSubscribeClient.c 4.0 KB

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