MQTTSubscribeServer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * Deserializes the supplied (wire) buffer into subscribe data
  22. * @param dup integer returned - the MQTT dup flag
  23. * @param packetid integer returned - the MQTT packet identifier
  24. * @param maxcount - the maximum number of members allowed in the
  25. * topicFilters and requestedQoSs arrays
  26. * @param count - number of members in the topicFilters and requestedQoSs
  27. * arrays
  28. * @param topicFilters - array of topic filter names
  29. * @param requestedQoSs - array of requested QoS
  30. * @param buf the raw buffer data, of the correct length determined by the
  31. * remaining length field
  32. * @param buflen the length in bytes of the data in the supplied buffer
  33. * @return the length of the serialized data. <= 0 indicates error
  34. */
  35. int MQTTDeserialize_subscribe(unsigned char* dup,
  36. unsigned short* packetid,
  37. int maxcount,
  38. int* count,
  39. MQTTString topicFilters[],
  40. int requestedQoSs[],
  41. unsigned char* buf,
  42. int buflen) {
  43. MQTTHeader header = {0};
  44. unsigned char* curdata = buf;
  45. unsigned char* enddata = NULL;
  46. int rc = -1;
  47. int mylen = 0;
  48. FUNC_ENTRY;
  49. header.byte = readChar(&curdata);
  50. if (header.bits.type != SUBSCRIBE)
  51. goto exit;
  52. *dup = header.bits.dup;
  53. curdata += (rc = MQTTPacket_decodeBuf(curdata,
  54. &mylen)); /* read remaining length */
  55. enddata = curdata + mylen;
  56. *packetid = readInt(&curdata);
  57. *count = 0;
  58. while (curdata < enddata) {
  59. if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
  60. goto exit;
  61. if (curdata >= enddata) /* do we have enough data to read the req_qos
  62. version byte? */
  63. goto exit;
  64. requestedQoSs[*count] = readChar(&curdata);
  65. (*count)++;
  66. }
  67. rc = 1;
  68. exit:
  69. FUNC_EXIT_RC(rc);
  70. return rc;
  71. }
  72. /**
  73. * Serializes the supplied suback data into the supplied buffer, ready for
  74. * sending
  75. * @param buf the buffer into which the packet will be serialized
  76. * @param buflen the length in bytes of the supplied buffer
  77. * @param packetid integer - the MQTT packet identifier
  78. * @param count - number of members in the grantedQoSs array
  79. * @param grantedQoSs - array of granted QoS
  80. * @return the length of the serialized data. <= 0 indicates error
  81. */
  82. int MQTTSerialize_suback(unsigned char* buf,
  83. int buflen,
  84. unsigned short packetid,
  85. int count,
  86. int* grantedQoSs) {
  87. MQTTHeader header = {0};
  88. int rc = -1;
  89. unsigned char* ptr = buf;
  90. int i;
  91. FUNC_ENTRY;
  92. if (buflen < 2 + count) {
  93. rc = MQTTPACKET_BUFFER_TOO_SHORT;
  94. goto exit;
  95. }
  96. header.byte = 0;
  97. header.bits.type = SUBACK;
  98. writeChar(&ptr, header.byte); /* write header */
  99. ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */
  100. writeInt(&ptr, packetid);
  101. for (i = 0; i < count; ++i)
  102. writeChar(&ptr, grantedQoSs[i]);
  103. rc = ptr - buf;
  104. exit:
  105. FUNC_EXIT_RC(rc);
  106. return rc;
  107. }