MQTTUnsubscribeServer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 unsubscribe 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 buf the raw buffer data, of the correct length determined by the remaining length field
  27. * @param buflen the length in bytes of the data in the supplied buffer
  28. * @return the length of the serialized data. <= 0 indicates error
  29. */
  30. int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
  31. unsigned char* buf, int len)
  32. {
  33. MQTTHeader header = {0};
  34. unsigned char* curdata = buf;
  35. unsigned char* enddata = NULL;
  36. int rc = 0;
  37. int mylen = 0;
  38. FUNC_ENTRY;
  39. header.byte = readChar(&curdata);
  40. if (header.bits.type != UNSUBSCRIBE)
  41. goto exit;
  42. *dup = header.bits.dup;
  43. curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
  44. enddata = curdata + mylen;
  45. *packetid = readInt(&curdata);
  46. *count = 0;
  47. while (curdata < enddata)
  48. {
  49. if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
  50. goto exit;
  51. (*count)++;
  52. }
  53. rc = 1;
  54. exit:
  55. FUNC_EXIT_RC(rc);
  56. return rc;
  57. }
  58. /**
  59. * Serializes the supplied unsuback data into the supplied buffer, ready for sending
  60. * @param buf the buffer into which the packet will be serialized
  61. * @param buflen the length in bytes of the supplied buffer
  62. * @param packetid integer - the MQTT packet identifier
  63. * @return the length of the serialized data. <= 0 indicates error
  64. */
  65. int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid)
  66. {
  67. MQTTHeader header = {0};
  68. int rc = 0;
  69. unsigned char *ptr = buf;
  70. FUNC_ENTRY;
  71. if (buflen < 2)
  72. {
  73. rc = MQTTPACKET_BUFFER_TOO_SHORT;
  74. goto exit;
  75. }
  76. header.byte = 0;
  77. header.bits.type = UNSUBACK;
  78. writeChar(&ptr, header.byte); /* write header */
  79. ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
  80. writeInt(&ptr, packetid);
  81. rc = ptr - buf;
  82. exit:
  83. FUNC_EXIT_RC(rc);
  84. return rc;
  85. }