subscribe_options.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file subscribe_options.h
  3. /// Declaration of MQTT subscribe_options class
  4. /// @date Aug 1, 2019 @
  5. /// @author Frank Pagliughi
  6. /////////////////////////////////////////////////////////////////////////////
  7. /*******************************************************************************
  8. * Copyright (c) 2019 Frank Pagliughi <fpagliughi@mindspring.com>
  9. *
  10. * All rights reserved. This program and the accompanying materials
  11. * are made available under the terms of the Eclipse Public License v1.0
  12. * and Eclipse Distribution License v1.0 which accompany this distribution.
  13. *
  14. * The Eclipse Public License is available at
  15. * http://www.eclipse.org/legal/epl-v10.html
  16. * and the Eclipse Distribution License is available at
  17. * http://www.eclipse.org/org/documents/edl-v10.php.
  18. *
  19. * Contributors:
  20. * Frank Pagliughi - initial implementation and documentation
  21. *******************************************************************************/
  22. #ifndef __mqtt_subscribe_options_h
  23. #define __mqtt_subscribe_options_h
  24. #include "MQTTAsync.h"
  25. #include "MQTTSubscribeOpts.h"
  26. #include "mqtt/types.h"
  27. namespace mqtt {
  28. /////////////////////////////////////////////////////////////////////////////
  29. /**
  30. * The MQTT v5 subscription options.
  31. * These are defined in section 3.8.3.1 of the MQTT v5 spec.
  32. * The defaults use the behavior that was present in MQTT v3.1.1.
  33. */
  34. class subscribe_options
  35. {
  36. /** The underlying C structure */
  37. MQTTSubscribe_options opts_;
  38. friend class async_client;
  39. friend class response_options;
  40. friend class subscribe_options_test;
  41. public:
  42. /** Smart/shared pointer to an object of this class. */
  43. using ptr_t = std::shared_ptr<subscribe_options>;
  44. /** Smart/shared pointer to a const object of this class. */
  45. using const_ptr_t = std::shared_ptr<const subscribe_options>;
  46. /** Don't receive our own publications */
  47. static constexpr bool SUBSCRIBE_NO_LOCAL = true;
  48. /** Receive our own publications */
  49. static constexpr bool SUBSCRIBE_LOCAL = false;
  50. /**
  51. * Retain flag is only set on publications sent by a broker if in
  52. * response to a subscribe request
  53. */
  54. static constexpr bool NO_RETAIN_AS_PUBLISHED = false;
  55. /** Keep the retain flag as on the original publish message */
  56. static constexpr bool RETAIN_AS_PUBLISHED = true;
  57. /** The options for subscription retain handling */
  58. enum RetainHandling {
  59. /** Send retained messages at the time of the subscribe */
  60. SEND_RETAINED_ON_SUBSCRIBE = 0,
  61. /** Send retained messages on subscribe only if subscription is new */
  62. SEND_RETAINED_ON_NEW = 1,
  63. /** Do not send retained messages at all */
  64. DONT_SEND_RETAINED = 2
  65. };
  66. /**
  67. * Create default subscription options.
  68. * These are the default options corresponding to the original MQTT (v3)
  69. * behaviors.
  70. */
  71. subscribe_options()
  72. : opts_(MQTTSubscribe_options_initializer) {}
  73. /**
  74. * Creates a set of subscription options.
  75. *
  76. * @param noLocal Whether the server should send back our own
  77. * publications, if subscribed.
  78. * @param retainAsPublished Whether to keep the retained flag as in the
  79. * original published message (true).
  80. * @param retainHandling When to send retained messages:
  81. * @li (SEND_RETAINED_ON_SUBSCRIBE, 0) At the time of the subscribe
  82. * @li (SEND_RETAINED_ON_NEW, 1) Only at the time of a new
  83. * subscribe
  84. * @li (DONT_SEND_RETAINED, 2) Not at the time of subscribe
  85. */
  86. explicit subscribe_options(bool noLocal, byte retainAsPublished=false,
  87. RetainHandling retainHandling=SEND_RETAINED_ON_SUBSCRIBE)
  88. : opts_(MQTTSubscribe_options_initializer) {
  89. opts_.noLocal = noLocal ? 1 : 0;
  90. opts_.retainAsPublished = retainAsPublished ? 1 : 0;
  91. opts_.retainHandling = (unsigned char) retainHandling;
  92. }
  93. /**
  94. * Gets the value of the "no local" flag.
  95. * @return Whether the server should send back our own publications, if
  96. * subscribed.
  97. */
  98. bool get_no_local() const {
  99. return to_bool(opts_.noLocal);
  100. }
  101. /**
  102. * Sets the "no local" flag on or off.
  103. * @param on Whether the server should send back our own publications,
  104. * if subscribed.
  105. */
  106. void set_no_local(bool on=true) {
  107. opts_.noLocal = on ? 1 : 0;
  108. }
  109. /**
  110. * Gets the "retain as published" flag.
  111. * @return Whether to keep the retained flag as in the original
  112. * published message.
  113. */
  114. bool get_retain_as_published() const {
  115. return to_bool(opts_.retainAsPublished);
  116. }
  117. /**
  118. * Sets the "retain as published" flag on or off.
  119. * @param on Whether to keep the retained flag as in the original
  120. * published message.
  121. */
  122. void set_retain_as_published(bool on) {
  123. opts_.retainAsPublished = on ? 1 : 0;
  124. }
  125. /**
  126. * Gets the "retasin handling" option.
  127. * @return When to send retained messages:
  128. * @li (SEND_RETAINED_ON_SUBSCRIBE, 0) At the time of the subscribe
  129. * @li (SEND_RETAINED_ON_NEW, 1) Only at the time of a new
  130. * subscribe
  131. * @li (DONT_SEND_RETAINED, 2) Not at the time of subscribe
  132. */
  133. auto get_retain_handling() const -> RetainHandling {
  134. return RetainHandling(opts_.retainHandling);
  135. }
  136. /**
  137. * Sets the "retain handling" option.
  138. * @param retainHandling When to send retained messages:
  139. * @li (SEND_RETAINED_ON_SUBSCRIBE, 0) At the time of the subscribe
  140. * @li (SEND_RETAINED_ON_NEW, 1) Only at the time of a new
  141. * subscribe
  142. * @li (DONT_SEND_RETAINED, 2) Not at the time of subscribe
  143. */
  144. void set_retain_handling(RetainHandling retainHandling) {
  145. opts_.retainHandling = (unsigned char) retainHandling;
  146. }
  147. };
  148. /** Smart/shared pointer to a subscribe options object. */
  149. using subscribe_options_ptr = subscribe_options::ptr_t;
  150. /////////////////////////////////////////////////////////////////////////////
  151. // end namespace mqtt
  152. }
  153. #endif // __mqtt_subscribe_options_h