MQTTUnsubscribeClient.c 3.3 KB

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