MQTTSubscribeServer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * 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 = -1;
  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. curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
  45. enddata = curdata + mylen;
  46. *packetid = readInt(&curdata);
  47. *count = 0;
  48. while (curdata < enddata)
  49. {
  50. if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
  51. goto exit;
  52. if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */
  53. goto exit;
  54. requestedQoSs[*count] = readChar(&curdata);
  55. (*count)++;
  56. }
  57. rc = 1;
  58. exit:
  59. FUNC_EXIT_RC(rc);
  60. return rc;
  61. }
  62. /**
  63. * Serializes the supplied suback data into the supplied buffer, ready for sending
  64. * @param buf the buffer into which the packet will be serialized
  65. * @param buflen the length in bytes of the supplied buffer
  66. * @param packetid integer - the MQTT packet identifier
  67. * @param count - number of members in the grantedQoSs array
  68. * @param grantedQoSs - array of granted QoS
  69. * @return the length of the serialized data. <= 0 indicates error
  70. */
  71. int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs)
  72. {
  73. MQTTHeader header = {0};
  74. int rc = -1;
  75. unsigned char *ptr = buf;
  76. int i;
  77. FUNC_ENTRY;
  78. if (buflen < 2 + count)
  79. {
  80. rc = MQTTPACKET_BUFFER_TOO_SHORT;
  81. goto exit;
  82. }
  83. header.byte = 0;
  84. header.bits.type = SUBACK;
  85. writeChar(&ptr, header.byte); /* write header */
  86. ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */
  87. writeInt(&ptr, packetid);
  88. for (i = 0; i < count; ++i)
  89. writeChar(&ptr, grantedQoSs[i]);
  90. rc = ptr - buf;
  91. exit:
  92. FUNC_EXIT_RC(rc);
  93. return rc;
  94. }