MQTTSubscribeServer.c 3.5 KB

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