MQTTProperties.h 9.3 KB

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