subscribe_options.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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-2023 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 v2.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-v20.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/platform.h"
  27. #include "mqtt/types.h"
  28. namespace mqtt {
  29. /////////////////////////////////////////////////////////////////////////////
  30. /**
  31. * The MQTT v5 subscription options.
  32. *
  33. * The subscribe options are bitfields in the payload of a SUBSCRIBE packet,
  34. * forming a single options byte for each topic filter in the subscription.
  35. *
  36. * These were added in MQTT v5. The default (zero/false) value for each
  37. * field gives the behavior that was present in MQTT v3.1.1. To get a new
  38. * behavior the field(s) must be set.
  39. *
  40. * These are defined in section 3.8.3.1 of the MQTT v5 spec.
  41. */
  42. class subscribe_options
  43. {
  44. /** The underlying C structure */
  45. MQTTSubscribe_options opts_;
  46. /** The client and response have special access */
  47. friend class async_client;
  48. friend class response_options;
  49. public:
  50. /** Smart/shared pointer to an object of this class. */
  51. using ptr_t = std::shared_ptr<subscribe_options>;
  52. /** Smart/shared pointer to a const object of this class. */
  53. using const_ptr_t = std::shared_ptr<const subscribe_options>;
  54. /** Don't receive our own publications */
  55. static constexpr bool NO_LOCAL = true;
  56. /** Receive our own publications */
  57. static constexpr bool LOCAL = false;
  58. /** @deprecated Don't receive our own publications */
  59. [[deprecated("Use NO_LOCAL")]] static constexpr bool SUBSCRIBE_NO_LOCAL = true;
  60. /** @deprecated Receive our own publications (obsolete name) */
  61. [[deprecated("Use LOCAL")]] static constexpr bool SUBSCRIBE_LOCAL = false;
  62. /**
  63. * Retain flag is only set on publications sent by a broker if in
  64. * response to a subscribe request
  65. */
  66. static constexpr bool NO_RETAIN_AS_PUBLISHED = false;
  67. /** Keep the retain flag as on the original publish message */
  68. static constexpr bool RETAIN_AS_PUBLISHED = true;
  69. /** The options for subscription retain handling */
  70. enum RetainHandling {
  71. /** Send retained messages at the time of the subscribe */
  72. SEND_RETAINED_ON_SUBSCRIBE = 0,
  73. /** Send retained messages on subscribe only if subscription is new */
  74. SEND_RETAINED_ON_NEW = 1,
  75. /** Do not send retained messages at all */
  76. DONT_SEND_RETAINED = 2
  77. };
  78. /**
  79. * Create default subscription options.
  80. * These are the default options corresponding to the original MQTT (v3)
  81. * behaviors.
  82. */
  83. subscribe_options() : opts_(MQTTSubscribe_options_initializer) {}
  84. /**
  85. * Creates a set of subscription options.
  86. *
  87. * @param noLocal Whether the server should send back our own
  88. * publications, if subscribed.
  89. * @param retainAsPublished Whether to keep the retained flag as in the
  90. * original published message (true).
  91. * @param retainHandling When to send retained messages:
  92. * @li (SEND_RETAINED_ON_SUBSCRIBE, 0) At the time of the subscribe
  93. * @li (SEND_RETAINED_ON_NEW, 1) Only at the time of a new
  94. * subscribe
  95. * @li (DONT_SEND_RETAINED, 2) Not at the time of subscribe
  96. */
  97. explicit subscribe_options(
  98. bool noLocal, bool retainAsPublished = false,
  99. RetainHandling retainHandling = SEND_RETAINED_ON_SUBSCRIBE
  100. )
  101. : opts_(MQTTSubscribe_options_initializer) {
  102. opts_.noLocal = noLocal ? 1 : 0;
  103. opts_.retainAsPublished = retainAsPublished ? 1 : 0;
  104. opts_.retainHandling = (unsigned char)retainHandling;
  105. }
  106. /**
  107. * Creates the set of subscribe options from an underlying C struct.
  108. * @param opts The Paho C subscribe options
  109. */
  110. explicit subscribe_options(MQTTSubscribe_options opts) : opts_{opts} {}
  111. #if defined(UNIT_TESTS)
  112. /**
  113. * Expose the underlying C struct for the unit tests.
  114. */
  115. const auto& c_struct() const { return opts_; }
  116. #endif
  117. /**
  118. * Gets the value of the "no local" flag.
  119. * @return Whether the server should send back our own publications, if
  120. * subscribed.
  121. */
  122. bool get_no_local() const { return to_bool(opts_.noLocal); }
  123. /**
  124. * Sets the "no local" flag on or off.
  125. * @param on Whether the server should send back our own publications,
  126. * if subscribed.
  127. */
  128. void set_no_local(bool on = true) { opts_.noLocal = on ? 1 : 0; }
  129. /**
  130. * Gets the "retain as published" flag.
  131. * @return Whether to keep the retained flag as in the original
  132. * published message.
  133. */
  134. bool get_retain_as_published() const { return to_bool(opts_.retainAsPublished); }
  135. /**
  136. * Sets the "retain as published" flag on or off.
  137. * @param on Whether to keep the retained flag as in the original
  138. * published message.
  139. */
  140. void set_retain_as_published(bool on = true) { opts_.retainAsPublished = on ? 1 : 0; }
  141. /**
  142. * Gets the "retain handling" option.
  143. * @return When to send retained messages:
  144. * @li (SEND_RETAINED_ON_SUBSCRIBE, 0) At the time of the subscribe
  145. * @li (SEND_RETAINED_ON_NEW, 1) Only at the time of a new
  146. * subscribe
  147. * @li (DONT_SEND_RETAINED, 2) Not at the time of subscribe
  148. */
  149. auto get_retain_handling() const -> RetainHandling {
  150. return RetainHandling(opts_.retainHandling);
  151. }
  152. /**
  153. * Sets the "retain handling" option.
  154. * @param retainHandling When to send retained messages:
  155. * @li (SEND_RETAINED_ON_SUBSCRIBE, 0) At the time of the subscribe
  156. * @li (SEND_RETAINED_ON_NEW, 1) Only at the time of a new
  157. * subscribe
  158. * @li (DONT_SEND_RETAINED, 2) Not at the time of subscribe
  159. */
  160. void set_retain_handling(RetainHandling retainHandling) {
  161. opts_.retainHandling = (unsigned char)retainHandling;
  162. }
  163. };
  164. /** Smart/shared pointer to a subscribe options object. */
  165. using subscribe_options_ptr = subscribe_options::ptr_t;
  166. /////////////////////////////////////////////////////////////////////////////
  167. } // namespace mqtt
  168. #endif // __mqtt_subscribe_options_h