MQTTUnsubscribeClient.c 3.6 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
  15. *documentation
  16. *******************************************************************************/
  17. #include "MQTTPacket.h"
  18. #include "StackTrace.h"
  19. #include <string.h>
  20. /**
  21. * Determines the length of the MQTT unsubscribe packet that would be
  22. * produced using the supplied parameters
  23. * @param count the number of topic filter strings in topicFilters
  24. * @param topicFilters the array of topic filter strings to be used in the
  25. * publish
  26. * @return the length of buffer needed to contain the serialized version of
  27. * the packet
  28. */
  29. int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[]) {
  30. int i;
  31. int len = 2; /* packetid */
  32. for (i = 0; i < count; ++i)
  33. len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/
  34. return len;
  35. }
  36. /**
  37. * Serializes the supplied unsubscribe data into the supplied buffer, ready for
  38. * sending
  39. * @param buf the raw buffer data, of the correct length determined by the
  40. * remaining length field
  41. * @param buflen the length in bytes of the data in the supplied buffer
  42. * @param dup integer - the MQTT dup flag
  43. * @param packetid integer - the MQTT packet identifier
  44. * @param count - number of members in the topicFilters array
  45. * @param topicFilters - array of topic filter names
  46. * @return the length of the serialized data. <= 0 indicates error
  47. */
  48. int MQTTSerialize_unsubscribe(unsigned char* buf,
  49. int buflen,
  50. unsigned char dup,
  51. unsigned short packetid,
  52. int count,
  53. MQTTString topicFilters[]) {
  54. unsigned char* ptr = buf;
  55. MQTTHeader header = {0};
  56. int rem_len = 0;
  57. int rc = -1;
  58. int i = 0;
  59. FUNC_ENTRY;
  60. if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(
  61. count, topicFilters)) > buflen) {
  62. rc = MQTTPACKET_BUFFER_TOO_SHORT;
  63. goto exit;
  64. }
  65. header.byte = 0;
  66. header.bits.type = UNSUBSCRIBE;
  67. header.bits.dup = dup;
  68. header.bits.qos = 1;
  69. writeChar(&ptr, header.byte); /* write header */
  70. ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */
  71. ;
  72. writeInt(&ptr, packetid);
  73. for (i = 0; i < count; ++i)
  74. writeMQTTString(&ptr, topicFilters[i]);
  75. rc = ptr - buf;
  76. exit:
  77. FUNC_EXIT_RC(rc);
  78. return rc;
  79. }
  80. /**
  81. * Deserializes the supplied (wire) buffer into unsuback data
  82. * @param packetid returned integer - the MQTT packet identifier
  83. * @param buf the raw buffer data, of the correct length determined by the
  84. * remaining length field
  85. * @param buflen the length in bytes of the data in the supplied buffer
  86. * @return error code. 1 is success, 0 is failure
  87. */
  88. int MQTTDeserialize_unsuback(unsigned short* packetid,
  89. unsigned char* buf,
  90. int buflen) {
  91. unsigned char type = 0;
  92. unsigned char dup = 0;
  93. int rc = 0;
  94. FUNC_ENTRY;
  95. rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
  96. if (type == UNSUBACK)
  97. rc = 1;
  98. FUNC_EXIT_RC(rc);
  99. return rc;
  100. }