subscribe_options.h 5.8 KB

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