MQTTProperties.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*******************************************************************************
  2. * Copyright (c) 2017, 2024 IBM Corp. and others
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. *******************************************************************************/
  16. #if !defined(MQTTPROPERTIES_H)
  17. #define MQTTPROPERTIES_H
  18. #include "MQTTExportDeclarations.h"
  19. #include <stdint.h>
  20. #define MQTT_INVALID_PROPERTY_ID -2
  21. /** The one byte MQTT V5 property indicator */
  22. enum MQTTPropertyCodes {
  23. MQTTPROPERTY_CODE_PAYLOAD_FORMAT_INDICATOR = 1, /**< The value is 1 */
  24. MQTTPROPERTY_CODE_MESSAGE_EXPIRY_INTERVAL = 2, /**< The value is 2 */
  25. MQTTPROPERTY_CODE_CONTENT_TYPE = 3, /**< The value is 3 */
  26. MQTTPROPERTY_CODE_RESPONSE_TOPIC = 8, /**< The value is 8 */
  27. MQTTPROPERTY_CODE_CORRELATION_DATA = 9, /**< The value is 9 */
  28. MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIER = 11, /**< The value is 11 */
  29. MQTTPROPERTY_CODE_SESSION_EXPIRY_INTERVAL = 17, /**< The value is 17 */
  30. MQTTPROPERTY_CODE_ASSIGNED_CLIENT_IDENTIFIER = 18,/**< The value is 18 */
  31. MQTTPROPERTY_CODE_ASSIGNED_CLIENT_IDENTIFER = 18,/**< The value is 18 (obsolete, misspelled) */
  32. MQTTPROPERTY_CODE_SERVER_KEEP_ALIVE = 19, /**< The value is 19 */
  33. MQTTPROPERTY_CODE_AUTHENTICATION_METHOD = 21, /**< The value is 21 */
  34. MQTTPROPERTY_CODE_AUTHENTICATION_DATA = 22, /**< The value is 22 */
  35. MQTTPROPERTY_CODE_REQUEST_PROBLEM_INFORMATION = 23,/**< The value is 23 */
  36. MQTTPROPERTY_CODE_WILL_DELAY_INTERVAL = 24, /**< The value is 24 */
  37. MQTTPROPERTY_CODE_REQUEST_RESPONSE_INFORMATION = 25,/**< The value is 25 */
  38. MQTTPROPERTY_CODE_RESPONSE_INFORMATION = 26, /**< The value is 26 */
  39. MQTTPROPERTY_CODE_SERVER_REFERENCE = 28, /**< The value is 28 */
  40. MQTTPROPERTY_CODE_REASON_STRING = 31, /**< The value is 31 */
  41. MQTTPROPERTY_CODE_RECEIVE_MAXIMUM = 33, /**< The value is 33*/
  42. MQTTPROPERTY_CODE_TOPIC_ALIAS_MAXIMUM = 34, /**< The value is 34 */
  43. MQTTPROPERTY_CODE_TOPIC_ALIAS = 35, /**< The value is 35 */
  44. MQTTPROPERTY_CODE_MAXIMUM_QOS = 36, /**< The value is 36 */
  45. MQTTPROPERTY_CODE_RETAIN_AVAILABLE = 37, /**< The value is 37 */
  46. MQTTPROPERTY_CODE_USER_PROPERTY = 38, /**< The value is 38 */
  47. MQTTPROPERTY_CODE_MAXIMUM_PACKET_SIZE = 39, /**< The value is 39 */
  48. MQTTPROPERTY_CODE_WILDCARD_SUBSCRIPTION_AVAILABLE = 40,/**< The value is 40 */
  49. MQTTPROPERTY_CODE_SUBSCRIPTION_IDENTIFIERS_AVAILABLE = 41,/**< The value is 41 */
  50. MQTTPROPERTY_CODE_SHARED_SUBSCRIPTION_AVAILABLE = 42/**< The value is 241 */
  51. };
  52. /**
  53. * Returns a printable string description of an MQTT V5 property code.
  54. * @param value an MQTT V5 property code.
  55. * @return the printable string description of the input property code.
  56. * NULL if the code was not found.
  57. */
  58. LIBMQTT_API const char* MQTTPropertyName(enum MQTTPropertyCodes value);
  59. /** The one byte MQTT V5 property type */
  60. enum MQTTPropertyTypes {
  61. MQTTPROPERTY_TYPE_BYTE,
  62. MQTTPROPERTY_TYPE_TWO_BYTE_INTEGER,
  63. MQTTPROPERTY_TYPE_FOUR_BYTE_INTEGER,
  64. MQTTPROPERTY_TYPE_VARIABLE_BYTE_INTEGER,
  65. MQTTPROPERTY_TYPE_BINARY_DATA,
  66. MQTTPROPERTY_TYPE_UTF_8_ENCODED_STRING,
  67. MQTTPROPERTY_TYPE_UTF_8_STRING_PAIR
  68. };
  69. /**
  70. * Returns the MQTT V5 type code of an MQTT V5 property.
  71. * @param value an MQTT V5 property code.
  72. * @return the MQTT V5 type code of the input property. -1 if the code was not found.
  73. */
  74. LIBMQTT_API int MQTTProperty_getType(enum MQTTPropertyCodes value);
  75. /**
  76. * The data for a length delimited string
  77. */
  78. typedef struct
  79. {
  80. int len; /**< the length of the string */
  81. char* data; /**< pointer to the string data */
  82. } MQTTLenString;
  83. /**
  84. * Structure to hold an MQTT version 5 property of any type
  85. */
  86. typedef struct
  87. {
  88. enum MQTTPropertyCodes identifier; /**< The MQTT V5 property id. A multi-byte integer. */
  89. /** The value of the property, as a union of the different possible types. */
  90. union {
  91. unsigned char byte; /**< holds the value of a byte property type */
  92. unsigned short integer2; /**< holds the value of a 2 byte integer property type */
  93. unsigned int integer4; /**< holds the value of a 4 byte integer property type */
  94. struct {
  95. MQTTLenString data; /**< The value of a string property, or the name of a user property. */
  96. MQTTLenString value; /**< The value of a user property. */
  97. };
  98. } value;
  99. } MQTTProperty;
  100. /**
  101. * MQTT version 5 property list
  102. */
  103. typedef struct MQTTProperties
  104. {
  105. int count; /**< number of property entries in the array */
  106. int max_count; /**< max number of properties that the currently allocated array can store */
  107. int length; /**< mbi: byte length of all properties */
  108. MQTTProperty *array; /**< array of properties */
  109. } MQTTProperties;
  110. #define MQTTProperties_initializer {0, 0, 0, NULL}
  111. /**
  112. * Returns the length of the properties structure when serialized ready for network transmission.
  113. * @param props an MQTT V5 property structure.
  114. * @return the length in bytes of the properties when serialized.
  115. */
  116. int MQTTProperties_len(const MQTTProperties* props);
  117. /**
  118. * Add a property pointer to the property array. Memory is allocated in this function,
  119. * so MQTTClient_create or MQTTAsync_create must be called first to initialize the
  120. * internal heap tracking. Alternatively MQTTAsync_global_init() can be called first
  121. * or build with the HIGH_PERFORMANCE option which disables the heap tracking.
  122. * @param props The property list to add the property to.
  123. * @param prop The property to add to the list.
  124. * @return 0 on success, -1 on failure.
  125. */
  126. LIBMQTT_API int MQTTProperties_add(MQTTProperties* props, const MQTTProperty* prop);
  127. /**
  128. * Serialize the given property list to a character buffer, e.g. for writing to the network.
  129. * @param pptr pointer to the buffer - move the pointer as we add data
  130. * @param properties pointer to the property list, can be NULL
  131. * @return whether the write succeeded or not: number of bytes written, or < 0 on failure.
  132. */
  133. int MQTTProperties_write(char** pptr, const MQTTProperties* properties);
  134. /**
  135. * Reads a property list from a character buffer into an array.
  136. * @param properties pointer to the property list to be filled. Should be initalized but empty.
  137. * @param pptr pointer to the character buffer.
  138. * @param enddata pointer to the end of the character buffer so we don't read beyond.
  139. * @return 1 if the properties were read successfully.
  140. */
  141. int MQTTProperties_read(MQTTProperties* properties, char** pptr, char* enddata);
  142. /**
  143. * Free all memory allocated to the property list, including any to individual properties.
  144. * @param properties pointer to the property list.
  145. */
  146. LIBMQTT_API void MQTTProperties_free(MQTTProperties* properties);
  147. /**
  148. * Copy the contents of a property list, allocating additional memory if needed.
  149. * @param props pointer to the property list.
  150. * @return the duplicated property list.
  151. */
  152. LIBMQTT_API MQTTProperties MQTTProperties_copy(const MQTTProperties* props);
  153. /**
  154. * Checks if property list contains a specific property.
  155. * @param props pointer to the property list.
  156. * @param propid the property id to check for.
  157. * @return 1 if found, 0 if not.
  158. */
  159. LIBMQTT_API int MQTTProperties_hasProperty(const MQTTProperties *props, enum MQTTPropertyCodes propid);
  160. /**
  161. * Returns the number of instances of a property id. Most properties can exist only once.
  162. * User properties and subscription ids can exist more than once.
  163. * @param props pointer to the property list.
  164. * @param propid the property id to check for.
  165. * @return the number of times found. Can be 0.
  166. */
  167. LIBMQTT_API int MQTTProperties_propertyCount(const MQTTProperties *props, enum MQTTPropertyCodes propid);
  168. /**
  169. * Returns the integer value of a specific property. The property given must be a numeric type.
  170. * @param props pointer to the property list.
  171. * @param propid the property id to check for.
  172. * @return the integer value of the property. -9999999 on failure.
  173. */
  174. LIBMQTT_API int64_t MQTTProperties_getNumericValue(const MQTTProperties *props, enum MQTTPropertyCodes propid);
  175. /**
  176. * Returns the integer value of a specific property when it's not the only instance.
  177. * The property given must be a numeric type.
  178. * @param props pointer to the property list.
  179. * @param propid the property id to check for.
  180. * @param index the instance number, starting at 0.
  181. * @return the integer value of the property. -9999999 on failure.
  182. */
  183. LIBMQTT_API int64_t MQTTProperties_getNumericValueAt(const MQTTProperties *props, enum MQTTPropertyCodes propid, int index);
  184. /**
  185. * Returns a pointer to the property structure for a specific property.
  186. * @param props pointer to the property list.
  187. * @param propid the property id to check for.
  188. * @return the pointer to the property structure if found. NULL if not found.
  189. */
  190. LIBMQTT_API MQTTProperty* MQTTProperties_getProperty(const MQTTProperties *props, enum MQTTPropertyCodes propid);
  191. /**
  192. * Returns a pointer to the property structure for a specific property when it's not the only instance.
  193. * @param props pointer to the property list.
  194. * @param propid the property id to check for.
  195. * @param index the instance number, starting at 0.
  196. * @return the pointer to the property structure if found. NULL if not found.
  197. */
  198. LIBMQTT_API MQTTProperty* MQTTProperties_getPropertyAt(const MQTTProperties *props, enum MQTTPropertyCodes propid, int index);
  199. #endif /* MQTTPROPERTIES_H */