will_options.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file will_options.h
  3. /// Declaration of MQTT will_options class
  4. /// @date Jul 7, 2016
  5. /// @author Guilherme M. Ferreira
  6. /////////////////////////////////////////////////////////////////////////////
  7. /*******************************************************************************
  8. * Copyright (c) 2016 Guilherme M. Ferreira <guilherme.maciel.ferreira@gmail.com>
  9. * Copyright (c) 2016-2023 Frank Pagliughi <fpagliughi@mindspring.com>
  10. *
  11. * All rights reserved. This program and the accompanying materials
  12. * are made available under the terms of the Eclipse Public License v2.0
  13. * and Eclipse Distribution License v1.0 which accompany this distribution.
  14. *
  15. * The Eclipse Public License is available at
  16. * http://www.eclipse.org/legal/epl-v20.html
  17. * and the Eclipse Distribution License is available at
  18. * http://www.eclipse.org/org/documents/edl-v10.php.
  19. *
  20. * Contributors:
  21. * Guilherme M. Ferreira - initial implementation and documentation
  22. * Frank Pagliughi - added copy & move operations
  23. *******************************************************************************/
  24. #ifndef __mqtt_will_options_h
  25. #define __mqtt_will_options_h
  26. #include "MQTTAsync.h"
  27. #include "mqtt/message.h"
  28. #include "mqtt/platform.h"
  29. #include "mqtt/topic.h"
  30. #include "mqtt/types.h"
  31. namespace mqtt {
  32. class connect_options;
  33. /////////////////////////////////////////////////////////////////////////////
  34. /**
  35. * Holds the set of options that govern the Last Will and Testament feature.
  36. *
  37. * @note
  38. * This wraps struct v1 of the C library's MQTTAsync_willOptions structure.
  39. * It sets the LWT binary payload, via the 'payload' struct field, and
  40. leaves the 'message' field as a nullptr.
  41. */
  42. class will_options
  43. {
  44. public:
  45. /** The default QoS for the LWT, if unspecified */
  46. static constexpr int DFLT_QOS = 0;
  47. /** The default retained flag for LWT, if unspecified */
  48. static constexpr bool DFLT_RETAINED = false;
  49. private:
  50. /** A default C struct to support re-initializing variables */
  51. static constexpr MQTTAsync_willOptions DFLT_C_STRUCT MQTTAsync_willOptions_initializer;
  52. /** The underlying C LWT options */
  53. MQTTAsync_willOptions opts_{DFLT_C_STRUCT};
  54. /** LWT message topic **/
  55. string_ref topic_;
  56. /** LWT message text */
  57. binary_ref payload_;
  58. /**
  59. * The properties for the LWT message.
  60. * Strangely, in the C lib, the will properties are not in the
  61. * willOptions struct, but are rather in the connectOptions.
  62. * So we keep the cached properties here, but need to transfer them to
  63. * the connect_options when we're added to that struct.
  64. */
  65. properties props_;
  66. /** The connect options has special access */
  67. friend class connect_options;
  68. /**
  69. * Gets a pointer to the C-language NUL-terminated strings for the
  70. * struct.
  71. * Some structs, such as this one, require valid pointers at all times,
  72. * while others expect NULL pointers for missing parameters.
  73. * So we always return a pointer to a valid C char array, even when
  74. * empty.
  75. * @param sr The C++ string object.
  76. * @return Pointer to a NUL terminated string. This is only valid until
  77. * the next time the string is updated. This is never nullptr.
  78. */
  79. const char* c_str(const string_ref& sr) { return sr ? sr.to_string().c_str() : nullptr; }
  80. public:
  81. /** Smart/shared pointer to an object of this class. */
  82. using ptr_t = std::shared_ptr<will_options>;
  83. /** Smart/shared pointer to a const object of this class. */
  84. using const_ptr_t = std::shared_ptr<const will_options>;
  85. /** Smart/shared pointer to an object of this class. */
  86. using unique_ptr_t = std::unique_ptr<will_options>;
  87. /**
  88. * Constructs a new object using the default values.
  89. */
  90. will_options();
  91. /**
  92. * Sets the "Last Will and Testament" (LWT) for the connection.
  93. * @param top The LWT message is published to the this topic.
  94. * @param payload The message that is published to the Will Topic.
  95. * @param payload_len The message size in bytes
  96. * @param qos The message Quality of Service.
  97. * @param retained Tell the broker to keep the LWT message after send to
  98. * subscribers.
  99. * @param props MQTT v5 properties for the will message.
  100. */
  101. will_options(
  102. string_ref top, const void* payload, size_t payload_len, int qos = DFLT_QOS,
  103. bool retained = DFLT_RETAINED, const properties& props = properties()
  104. );
  105. /**
  106. * Sets the "Last Will and Testament" (LWT) for the connection.
  107. * @param top The LWT message is published to the this topic.
  108. * @param payload The message that is published to the Will Topic.
  109. * @param payload_len The message size in bytes.
  110. * @param qos The message Quality of Service.
  111. * @param retained Tell the broker to keep the LWT message after send to
  112. * subscribers.
  113. * @param props MQTT v5 properties for the will message.
  114. */
  115. will_options(
  116. const topic& top, const void* payload, size_t payload_len, int qos = DFLT_QOS,
  117. bool retained = DFLT_RETAINED, const properties& props = properties()
  118. );
  119. /**
  120. * Sets the "Last Will and Testament" (LWT) for the connection.
  121. * @param top The LWT message is published to the this topic.
  122. * @param payload The message payload that is published to the Will
  123. * Topic.
  124. * @param qos The message Quality of Service.
  125. * @param retained Tell the broker to keep the LWT message after send to
  126. * subscribers.
  127. * @param props MQTT v5 properties for the will message.
  128. */
  129. will_options(
  130. string_ref top, binary_ref payload, int qos = DFLT_QOS, bool retained = DFLT_RETAINED,
  131. const properties& props = properties()
  132. );
  133. /**
  134. * Sets the "Last Will and Testament" (LWT) for the connection.
  135. * @param top The LWT message is published to the this topic.
  136. * @param payload The message payload that is published to the Will
  137. * Topic, as a string.
  138. * @param qos The message Quality of Service.
  139. * @param retained Tell the broker to keep the LWT message after send to
  140. * subscribers.
  141. * @param props MQTT v5 properties for the will message.
  142. */
  143. will_options(
  144. string_ref top, const string& payload, int qos = DFLT_QOS, bool retained = DFLT_QOS,
  145. const properties& props = properties()
  146. );
  147. /**
  148. * Sets the "Last Will and Testament" (LWT) for the connection.
  149. * @param msg The message that is published to the Will Topic.
  150. */
  151. will_options(const message& msg);
  152. /**
  153. * Copy constructor for the LWT options.
  154. * @param opt The other options.
  155. */
  156. will_options(const will_options& opt);
  157. /**
  158. * Move constructor for the LWT options.
  159. * @param opt The other options.
  160. */
  161. will_options(will_options&& opt);
  162. /**
  163. * Copy assignment for the LWT options.
  164. * @param opt The other options.
  165. */
  166. will_options& operator=(const will_options& opt);
  167. /**
  168. * Move assignment for the LWT options.
  169. * @param opt The other options.
  170. */
  171. will_options& operator=(will_options&& opt);
  172. /**
  173. * Expose the underlying C struct for the unit tests.
  174. */
  175. #if defined(UNIT_TESTS)
  176. const MQTTAsync_willOptions& c_struct() const { return opts_; }
  177. #endif
  178. /**
  179. * Gets the LWT message topic name.
  180. * @return The LWT message topic name.
  181. */
  182. string get_topic() const { return topic_ ? topic_.to_string() : string(); }
  183. /**
  184. * Gets the LWT message payload.
  185. * @return The LWT message payload.
  186. */
  187. const binary_ref& get_payload() const { return payload_; }
  188. /**
  189. * Gets the LWT message payload as a string.
  190. * @return The LWT message payload as a string.
  191. */
  192. string get_payload_str() const { return payload_ ? payload_.to_string() : string(); }
  193. /**
  194. * Gets the QoS value for the LWT message.
  195. * @return The QoS value for the LWT message.
  196. */
  197. int get_qos() const { return opts_.qos; }
  198. /**
  199. * Gets the 'retained' flag for the LWT message.
  200. * @return The 'retained' flag for the LWT message.
  201. */
  202. bool is_retained() const { return opts_.retained != 0; }
  203. /**
  204. * Gets the LWT message as a message object.
  205. * @return A pointer to a copy of the LWT message.
  206. */
  207. const_message_ptr get_message() const {
  208. return message::create(topic_, payload_, opts_.qos, to_bool(opts_.retained));
  209. }
  210. /**
  211. * Sets the LWT message topic name.
  212. * @param top The topic where to sent the message
  213. */
  214. void set_topic(string_ref top);
  215. /**
  216. * Sets the LWT message text.
  217. * @param msg The LWT message
  218. */
  219. void set_payload(binary_ref msg);
  220. /**
  221. * Sets the LWT message text.
  222. * @param msg The LWT message
  223. */
  224. void set_payload(string msg) { set_payload(binary_ref(std::move(msg))); }
  225. /**
  226. * Sets the QoS value.
  227. * @param qos The LWT message QoS
  228. */
  229. void set_qos(const int qos) { opts_.qos = qos; }
  230. /**
  231. * Sets the retained flag.
  232. * @param retained Tell the broker to keep the LWT message after send to
  233. * subscribers.
  234. */
  235. void set_retained(bool retained) { opts_.retained = to_int(retained); }
  236. /**
  237. * Gets the connect properties.
  238. * @return A const reference to the properties for the connect.
  239. */
  240. const properties& get_properties() const { return props_; }
  241. /**
  242. * Sets the properties for the connect.
  243. * @param props The properties to place into the message.
  244. */
  245. void set_properties(const properties& props) { props_ = props; }
  246. /**
  247. * Moves the properties for the connect.
  248. * @param props The properties to move into the connect object.
  249. */
  250. void set_properties(properties&& props) { props_ = std::move(props); }
  251. };
  252. /** Shared pointer to a will options object. */
  253. using will_options_ptr = will_options::ptr_t;
  254. /** Shared pointer to a const will options object. */
  255. using const_will_options_ptr = will_options::const_ptr_t;
  256. /** Unique pointer to a will options object. */
  257. using will_options_unique_ptr = will_options::unique_ptr_t;
  258. /////////////////////////////////////////////////////////////////////////////
  259. } // namespace mqtt
  260. #endif // __mqtt_will_options_h