properties.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /////////////////////////////////////////////////////////////////////////////
  2. /// @file properties.h
  3. /// Declaration of MQTT properties class
  4. /// @date July 7, 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_properties_h
  23. #define __mqtt_properties_h
  24. extern "C" {
  25. #include "MQTTProperties.h"
  26. }
  27. #include "mqtt/types.h"
  28. #include "mqtt/buffer_ref.h"
  29. #include "mqtt/exception.h"
  30. #include "mqtt/platform.h"
  31. #include <tuple>
  32. #include <initializer_list>
  33. #include <iostream>
  34. namespace mqtt {
  35. /** A pair of strings as a tuple. */
  36. using string_pair = std::tuple<string, string>;
  37. /////////////////////////////////////////////////////////////////////////////
  38. /**
  39. * A single MQTT v5 property.
  40. */
  41. class property
  42. {
  43. /** The underlying Paho C property struct. */
  44. MQTTProperty prop_;
  45. // Make a deep copy of the property struct into this one.
  46. // For string properties, this allocates memory and copied the string(s)
  47. void copy(const MQTTProperty& other);
  48. public:
  49. /**
  50. * The integer codes for the different v5 properties.
  51. */
  52. enum code {
  53. PAYLOAD_FORMAT_INDICATOR = 1,
  54. MESSAGE_EXPIRY_INTERVAL = 2,
  55. CONTENT_TYPE = 3,
  56. RESPONSE_TOPIC = 8,
  57. CORRELATION_DATA = 9,
  58. SUBSCRIPTION_IDENTIFIER = 11,
  59. SESSION_EXPIRY_INTERVAL = 17,
  60. ASSIGNED_CLIENT_IDENTIFER = 18,
  61. SERVER_KEEP_ALIVE = 19,
  62. AUTHENTICATION_METHOD = 21,
  63. AUTHENTICATION_DATA = 22,
  64. REQUEST_PROBLEM_INFORMATION = 23,
  65. WILL_DELAY_INTERVAL = 24,
  66. REQUEST_RESPONSE_INFORMATION = 25,
  67. RESPONSE_INFORMATION = 26,
  68. SERVER_REFERENCE = 28,
  69. REASON_STRING = 31,
  70. RECEIVE_MAXIMUM = 33,
  71. TOPIC_ALIAS_MAXIMUM = 34,
  72. TOPIC_ALIAS = 35,
  73. MAXIMUM_QOS = 36,
  74. RETAIN_AVAILABLE = 37,
  75. USER_PROPERTY = 38,
  76. MAXIMUM_PACKET_SIZE = 39,
  77. WILDCARD_SUBSCRIPTION_AVAILABLE = 40,
  78. SUBSCRIPTION_IDENTIFIERS_AVAILABLE = 41,
  79. SHARED_SUBSCRIPTION_AVAILABLE = 42
  80. };
  81. /**
  82. * Create a numeric property.
  83. * This can be a byte, or 2-byte, 4-byte, or variable byte integer.
  84. * @param c The property code
  85. * @param val The integer value for the property
  86. */
  87. property(code c, int32_t val);
  88. /**
  89. * Create a string or binary property.
  90. * @param c The property code
  91. * @param val The value for the property
  92. */
  93. property(code c, string_ref val);
  94. /**
  95. * Create a string pair property.
  96. * @param c The property code
  97. * @param name The string name for the property
  98. * @param val The string value for the property
  99. */
  100. property(code c, string_ref name, string_ref val);
  101. /**
  102. * Creates a property list from an C struct.
  103. * @param cprop A C struct for a property list.
  104. */
  105. explicit property(const MQTTProperty& cprop);
  106. /**
  107. * Moves a C struct into this property list.
  108. * This takes ownership of any memory that the C struct is holding.
  109. * @param cprop A C struct for a property list.
  110. */
  111. explicit property(MQTTProperty&& cprop);
  112. /**
  113. * Copy constructor
  114. * @param other The other property to copy into this one.
  115. */
  116. property(const property& other);
  117. /**
  118. * Move constructor.
  119. * @param other The other property that is moved into this one.
  120. */
  121. property(property&& other);
  122. /**
  123. * Destructor
  124. */
  125. ~property();
  126. /**
  127. * Copy assignment.
  128. * @param rhs Another property list to copy into this one.
  129. * @return A reference to this object.
  130. */
  131. property& operator=(const property& rhs);
  132. /**
  133. * Move assignment.
  134. * @param rhs Another property list to move into this one.
  135. * @return A reference to this object.
  136. */
  137. property& operator=(property&& rhs);
  138. /**
  139. * Gets the underlying C property struct.
  140. * @return A const reference to the underlying C property
  141. * struct.
  142. */
  143. const MQTTProperty& c_struct() const { return prop_; }
  144. /**
  145. * Gets the property type (identifier).
  146. * @return The code for the property type.
  147. */
  148. code type() const { return code(prop_.identifier); }
  149. /**
  150. * Gets a printable name for the property type.
  151. * @return A printable name for the property type.
  152. */
  153. const char* type_name() const {
  154. return ::MQTTPropertyName(prop_.identifier);
  155. }
  156. };
  157. /**
  158. * Extracts the value from the property as the specified type.
  159. * @return The value from the property as the specified type.
  160. */
  161. template <typename T>
  162. inline T get(const property&) { throw bad_cast(); }
  163. /**
  164. * Extracts the value from the property as an unsigned 8-bit integer.
  165. * @return The value from the property as an unsigned 8-bit integer.
  166. */
  167. template <>
  168. inline uint8_t get<uint8_t>(const property& prop) {
  169. return (uint8_t) prop.c_struct().value.byte;
  170. }
  171. /**
  172. * Extracts the value from the property as an unsigned 16-bit integer.
  173. * @return The value from the property as an unsigned 16-bit integer.
  174. */
  175. template <>
  176. inline uint16_t get<uint16_t>(const property& prop) {
  177. return (uint16_t) prop.c_struct().value.integer2;
  178. }
  179. /**
  180. * Extracts the value from the property as a signed 16-bit integer.
  181. * @return The value from the property as a signed 16-bit integer.
  182. */
  183. template <>
  184. inline int16_t get<int16_t>(const property& prop) {
  185. return (int16_t) prop.c_struct().value.integer2;
  186. }
  187. /**
  188. * Extracts the value from the property as an unsigned 32-bit integer.
  189. * @return The value from the property as an unsigned 32-bit integer.
  190. */
  191. template <>
  192. inline uint32_t get<uint32_t>(const property& prop) {
  193. return (uint32_t) prop.c_struct().value.integer4;
  194. }
  195. /**
  196. * Extracts the value from the property as a signed 32-bit integer.
  197. * @return The value from the property as a signed 32-bit integer.
  198. */
  199. template <>
  200. inline int32_t get<int32_t>(const property& prop) {
  201. return (int32_t) prop.c_struct().value.integer4;
  202. }
  203. /**
  204. * Extracts the value from the property as a string.
  205. * @return The value from the property as a string.
  206. */
  207. template <>
  208. inline string get<string>(const property& prop) {
  209. return (!prop.c_struct().value.data.data) ? string()
  210. : string(prop.c_struct().value.data.data, prop.c_struct().value.data.len);
  211. }
  212. /**
  213. * Extracts the value from the property as a pair of strings.
  214. * @return The value from the property as a pair of strings.
  215. */
  216. template <>
  217. inline string_pair get<string_pair>(const property& prop) {
  218. string name = (!prop.c_struct().value.data.data) ? string()
  219. : string(prop.c_struct().value.data.data, prop.c_struct().value.data.len);
  220. string value = (!prop.c_struct().value.value.data) ? string()
  221. : string(prop.c_struct().value.value.data, prop.c_struct().value.value.len);
  222. return std::make_tuple(std::move(name), std::move(value));
  223. }
  224. /////////////////////////////////////////////////////////////////////////////
  225. /**
  226. * MQTT v5 property list.
  227. *
  228. * A collection of properties that can be added to outgoing packets or
  229. * retrieved from incoming packets.
  230. */
  231. class properties
  232. {
  233. /** The default C struct */
  234. PAHO_MQTTPP_EXPORT static const MQTTProperties DFLT_C_STRUCT;
  235. /** The underlying C properties struct */
  236. MQTTProperties props_;
  237. template<typename T>
  238. friend T get(const properties& props, property::code propid, size_t idx);
  239. template<typename T>
  240. friend T get(const properties& props, property::code propid);
  241. public:
  242. /**
  243. * Default constructor.
  244. * Creates an empty properties list.
  245. */
  246. properties();
  247. /**
  248. * Copy constructor.
  249. * @param other The property list to copy.
  250. */
  251. properties(const properties& other)
  252. : props_(::MQTTProperties_copy(&other.props_)) {}
  253. /**
  254. * Move constructor.
  255. * @param other The property list to move to this one.
  256. */
  257. properties(properties&& other) : props_(other.props_) {
  258. std::memset(&other.props_, 0, sizeof(MQTTProperties));
  259. }
  260. /**
  261. * Creates a list of properties from a C struct.
  262. * @param cprops The c struct of properties
  263. */
  264. properties(const MQTTProperties& cprops) {
  265. props_ = ::MQTTProperties_copy(&cprops);
  266. }
  267. /**
  268. * Constructs from a list of property objects.
  269. * @param props An initializer list of property objects.
  270. */
  271. properties(std::initializer_list<property> props);
  272. /**
  273. * Destructor.
  274. */
  275. ~properties() { ::MQTTProperties_free(&props_); }
  276. /**
  277. * Gets a reference to the underlying C properties structure.
  278. * @return A const reference to the underlying C properties structure.
  279. */
  280. const MQTTProperties& c_struct() const { return props_; }
  281. /**
  282. * Copy assignment.
  283. * @param rhs The other property list to copy into this one
  284. * @return A reference to this object.
  285. */
  286. properties& operator=(const properties& rhs);
  287. /**
  288. * Move assignment.
  289. * @param rhs The property list to move to this one.
  290. * @return A reference to this object.
  291. */
  292. properties& operator=(properties&& rhs);
  293. /**
  294. * Determines if the property list is empty.
  295. * @return @em true if there are no properties in the list, @em false if
  296. * the list contains any items.
  297. */
  298. bool empty() const { return props_.count == 0; }
  299. /**
  300. * Gets the numbers of property items in the list.
  301. * @return The number of property items in the list.
  302. */
  303. size_t size() const { return size_t(props_.count); }
  304. /**
  305. * Adds a property to the list.
  306. * @param prop The property to add to the list.
  307. */
  308. void add(const property& prop) {
  309. ::MQTTProperties_add(&props_, &prop.c_struct());
  310. }
  311. /**
  312. * Removes all the items from the property list.
  313. */
  314. void clear() {
  315. ::MQTTProperties_free(&props_);
  316. }
  317. /**
  318. * Determines if the list contains a specific property.
  319. * @param propid The property ID (code).
  320. * @return @em true if the list contains the property, @em false if not.
  321. */
  322. bool contains(property::code propid) const {
  323. return ::MQTTProperties_hasProperty(const_cast<MQTTProperties*>(&props_),
  324. MQTTPropertyCodes(propid)) != 0;
  325. }
  326. /**
  327. * Get the number of properties in the list with the specified property
  328. * ID.
  329. *
  330. * Most properties can exist only once. User properties and subscription
  331. * ID's can exist more than once.
  332. *
  333. * @param propid The property ID (code).
  334. * @return The number of properties in the list with the specified ID.
  335. */
  336. size_t count(property::code propid) const {
  337. return size_t(::MQTTProperties_propertyCount(
  338. const_cast<MQTTProperties*>(&props_), MQTTPropertyCodes(propid)));
  339. }
  340. /**
  341. * Gets the property with the specified ID.
  342. *
  343. * @param propid The property ID (code).
  344. * @param idx Which instance of the property to retrieve, if there are
  345. * more than one.
  346. * @return The requested property
  347. */
  348. property get(property::code propid, size_t idx=0);
  349. };
  350. // --------------------------------------------------------------------------
  351. /**
  352. * Retrieves a single value from a property list for when there may be
  353. * multiple identical property ID's.
  354. * @tparam T The type of the value to retrieve
  355. * @param props The property list
  356. * @param propid The property ID code for the desired value.
  357. * @param idx Index of the desired property ID
  358. * @return The requested value of type T
  359. */
  360. template<typename T>
  361. inline T get(const properties& props, property::code propid, size_t idx)
  362. {
  363. MQTTProperty* prop = MQTTProperties_getPropertyAt(
  364. const_cast<MQTTProperties*>(&props.c_struct()),
  365. MQTTPropertyCodes(propid), int(idx));
  366. if (!prop)
  367. throw bad_cast();
  368. return get<T>(property(*prop));
  369. }
  370. /**
  371. * Retrieves a single value from a property list.
  372. * @tparam T The type of the value to retrieve
  373. * @param props The property list
  374. * @param propid The property ID code for the desired value.
  375. * @return The requested value of type T
  376. */
  377. template<typename T>
  378. inline T get(const properties& props, property::code propid)
  379. {
  380. return get<T>(props, propid, 0);
  381. }
  382. /////////////////////////////////////////////////////////////////////////////
  383. // end namespace mqtt
  384. }
  385. #endif // __mqtt_properties_h